我已经使用firebase和angularjs创建了一个身份验证过程。
在身份验证过程之后,页面必须重定向到htmlnew.html
使用$routeprovider
。但它在身份验证后不会重定向。
的index.html
<html ng-app="appName">
<body>
<div ng-controller="loginCtrl" id="message">
<a ng-click="googleSignin()">SignIn into App</a>
</div>
<p id="load">Firebase SDK Loading…</p>
</body>
</html>
htmlnew.html
<body>
<div id="message" ng-controller="loginpage">
<h1>Welcome</h1>
<a ng-click="googleSignout()">Signout</a>
</div>
</body>
app.js
var app = angular.module("appName", ["ngRoute", "firebase"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: 'public/index.html',
controller: 'loginCtrl'
})
.when('/login', {
templateUrl: 'public/htmlnew.html',
controller: 'loginpage'
})
});
app.controller('loginCtrl', function ($scope) {
var provider = new firebase.auth.GoogleAuthProvider();
$scope.googleSignin = function () {
firebase.auth()
.signInWithPopup(provider).then(function (result) {
var token = result.credential.accessToken;
var user = result.user;
console.log(token);
console.log(user);
}).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
}
});
app.controller('loginpage', function ($scope) {
$scope.googleSignout = function () {
firebase.auth().signOut()
.then(function () {
console.log('Signout Succesfull');
}, function (error) {
console.log('Signout Failed');
});
}
});
项目文档
有关此项目link
的演示检查提前致谢
答案 0 :(得分:1)
使用$location
重定向到路径。将其注入控制器依赖项并使用如下
firebase.auth()
.signInWithPopup(provider).then(function (result) {
$location.path('/login');
})
答案 1 :(得分:1)
对于成功signwithpopup后的重定向,你应该在function(result)
中提供状态更改,如果在function(error)
<强> $ location.path(&#39; /登录); 强>
因为你正在使用ngRoute。你可以使用$location.path
来传递你的州。
希望这会对你有所帮助。
app.controller('loginCtrl', function ($scope,$location) {
var provider = new firebase.auth.GoogleAuthProvider();
$scope.googleSignin = function () {
firebase.auth()
.signInWithPopup(provider).then(function (result) {
var token = result.credential.accessToken;
var user = result.user;
$location.path('/login');
console.log(token);
console.log(user);
}).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
}
});
答案 2 :(得分:1)
firebase.auth()
.signInWithPopup(provider).then(function (result) {
var token = result.credential.accessToken;
var user = result.user;
// You missed this $location.path('/pathYouNeed ');