简体   繁体   English

首次登录angular js后,如何自动将页面从“ /”路由重定向到主“ / dashboard”路由?

[英]How to redirect a page from '/' route to main '/dashboard' route automatically after first sign in angular js?

I have auth.js page which decides which route to direct to after login.I am using ui-router for routing.Now the thing is after login user details are captured and the route accordingly changes.Here if I change the route from '/dashboard' to '/' and hit enter, then it first shows sign in page then redirects to dashboard which I don't want.If I go from '/dashboard' to '/' then it should automatically go to dashboard page without going to sign in page.How do I achieve this? 我有auth.js页面,该页面决定登录后要定向到的路由。我正在使用ui-router进行路由。现在事情是在登录用户详细信息被捕获之后,路由也相应更改了。如果我从'/更改路由仪表板''到'/'并按Enter键,然后它首先显示登录页面,然后重定向到不需要的仪表板。如果我从'/ dashboard'转到'/',则它应该自动转到仪表板页面登录页面。如何实现?

Thanks in advance 提前致谢

In your auth.js when user signIn (callback after user signIn success) 用户登录时在auth.js中(用户登录成功后进行回调)

//In SignIn success callback.

$state.go('dashboard');//for state
$location.path("/dashboard");//for url dashboard

Hope it helps 希望能帮助到你

You can use resolve to address this issue. 您可以使用resolve解决此问题。 When you are redirecting to "/" page. 当您重定向到“ /”页面时。 Just check in resolve section, whether the user is logged in or not. 只需在“解决”部分中检查用户是否已登录即可。 If the user is logged in, redirect to dashboard page. 如果用户已登录,请重定向到仪表板页面。

then it first shows sign in page then redirects to dashboard which I don't want.If I go from '/dashboard' to '/' then it should automatically go to dashboard page without going to sign in page.How do I achieve this 然后它首先显示登录页面,然后重定向到我不想要的仪表板。如果我从'/ dashboard'转到'/',那么它应该自动转到仪表板页面而无需登录页面。我该如何实现

Really depends on your code. 真的取决于您的代码。 I recommend having the login route / logic maintained by the backend (check credentials there, and send a redirect to /login if not logged in.) 我建议由后端维护login路由/逻辑(在此处检查凭据,如果未登录,则将重定向发送到/login 。)

<!--HTML code-->
<div ng-init="CheckIfLoggedIn()">
  <form id="login-form"> 
  ..............
  </form>
</div>

//in angular JS
$scope.CheckIfLoggedIn = function(){
    if (loggedinuser){
        $location.path("/dashboard");
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM