如果用户已经使用Firebase进行了身份验证,我正尝试在初始加载时将我的应用直接发送到信息中心。
基本上,如果用户已登录。我希望他们在手机上打开应用程序时看到仪表板而不是登录页面。
我已经尝试将其放在我的app.run()
中。它工作并做我想要的,但它导致一个丑陋的错误
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []
在停止前大约运行20次。
app.run()
函数中的代码:
$rootScope.$on("$stateChangeStart", function(event, next, current) {
if($rootScope.user)
{
if(next.name === 'login')
{
$state.go('tab.dash');
event.preventDefault();
}
else
{
console.log('Do Nothing, User Is Not Logged In');
}
}
});
如何在不收到任何错误的情况下执行此操作?
答案 0 :(得分:0)
尝试将代码放在$ ionicPlatform.ready中(不带$ stateChangeStart)。
我正在使用Parse作为我的应用程序,但这是我的代码。
.run(function($state, $ionicPlatform, $rootScope) {
$ionicPlatform.ready(function() {
var currentUser = Parse.User.current();
$rootScope.isLoggedIn = false;
if (currentUser) {
$state.go('tab.home');
$rootScope.isLoggedIn = true;
} else {
$state.go('login');
}
});
})
答案 1 :(得分:0)
在https://devdactic.com/user-auth-angularjs-ionic/
上找到了这个
$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get("$state");
var $rootScope = $injector.get("$rootScope");
if ($rootScope.user) {
$state.go("app.dashboard");
} else {
$state.go("app.login");
}
});

这应该可以解决你的问题