【发布时间】:2017-12-15 19:48:57
【问题描述】:
我在我的应用中加入了身份验证,使用的是 Google 的 Firebase。我一直在关注他们的文档https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html。尽管此方法使用stateChange,但据我所知,ui-router 已弃用此方法。所以在网上看我把我的脚本从这个例子变成了:
app.run(["$rootScope", "$state", function($rootScope, $state) {
$rootScope.$on("$stateChangeError",
function(event, toState, toParams, fromState, fromParams, error) {
console.log('HELLO')
if (error === "AUTH_REQUIRED") {
$state.go("login");
}
});
}]);
到:
app.run(($transitions, $state) => {
$transitions.onError({}, ($transition$) => {
if ($transition$.$to().name !== 'init' && $transition$.$from().name !== 'error') {
$state.go("login");
}
});
});
我正在尝试处理该错误,因此当用户尝试访问需要身份验证的页面时,它会将他们引导回登录屏幕。第二种方法是有效的,当它尝试将状态更改为 login 时会冻结。
问题
当用户尝试访问需要身份验证的页面时,为什么我的脚本无法将用户重定向到 login 页面?
app.config(['$stateProvider', function($stateProvider, ) {
$stateProvider
.state('login', {
url: '/login',
component: 'login'
})
.state('patents', {
url: '/patents',
component: 'patents',
resolve: {
currentAuth: function(authentication) {
return authentication.requireAuth();
}
});
}]);
app.factory('authentication', ['$rootScope', '$location', '$firebaseAuth', '$firebaseObject', function($rootScope, $location, $firebaseAuth, $firebaseObject) {
var auth = $firebaseAuth();
//LOAD OF OTHER AUTHENTICATION METHODS
return {
requireAuth: function() {
return auth.$requireSignIn();
}
}
}])
【问题讨论】:
标签: angularjs firebase angular-ui-router firebase-authentication