[英]Redirect to dashboard if user is logged in with Ionic
I am trying to make my app direct to the dashboard on initial load if the user has authenticated with Firebase already. 如果用户已通过Firebase进行身份验证,则尝试在初始加载时将我的应用程序直接定向到仪表板。
Basically, if the user has logged in. I want them to see the dashboard instead of the login page when they open the app on their phone. 基本上,如果用户已登录。我希望他们在手机上打开应用程序时看到仪表板,而不是登录页面。
I've tried to place this in my app.run()
. 我试图将其放置在我的
app.run()
。 It works and does what I want but it results in an ugly error 它可以工作并且可以执行我想要的操作,但是会导致一个难看的错误
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []
This runs about 20 times before stopping. 在停止之前,此过程运行约20次。
My code in the app.run()
function: 我在
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');
}
}
});
How do I do this without getting any errors? 我如何做到这一点而不会出现任何错误?
Try placing your code within $ionicPlatform.ready (without $stateChangeStart). 尝试将代码放在$ ionicPlatform.ready中(不带$ stateChangeStart)。
I'm using Parse for my app but here is my code. 我正在为我的应用程序使用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');
}
});
})
Found this on https://devdactic.com/user-auth-angularjs-ionic/ 在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"); } });
That should solve your problem 那应该解决你的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.