简体   繁体   English

如果用户已登录,则角度重定向到仪表板

[英]Angular redirect to dashboard if user is logged in

I am using Angular v6, I have implement redirect from dashboard to login page if user is not logged in. 我正在使用Angular v6,如果用户没有登录,我已经实现了从仪表板到登录页面的重定向。
I'm confused between the two options: 我在两个选项之间感到困惑:

  1. Add a login guard file to check login status and redirect to dashboard 添加登录保护文件以检查登录状态并重定向到仪表板
  2. Detect route changing event, check login status here and redirect 检测路由更改事件,在此处检查登录状态并重定向

What is the better solution for this case ? 这个案例有什么更好的解决方案?

Use Authentication Guard in your routing module It will make things easy for you to check whether a user is login or not 在路由模块中使用Authentication Guard这将使您可以轻松检查用户是否登录

For a better understanding of Auth Guard here are some links that might help you : 为了更好地理解Auth Guard,以下是一些可能对您有所帮助的链接:

http://jasonwatmore.com/post/2018/05/23/angular-6-jwt-authentication-example-tutorial http://jasonwatmore.com/post/2018/05/23/angular-6-jwt-authentication-example-tutorial

How to use angular 6 Route Auth Guards for all routes Root and Child Routes? 如何使用angular 6 Route Auth Guards用于所有路由Root和Child Routes?

https://medium.com/@ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3 https://medium.com/@ryanchenkie_40935/angular-authentication-using-route-guards-bf7a4ca13ae3

https://scotch.io/courses/routing-angular-2-applications/canactivate-and-canactivatechild https://scotch.io/courses/routing-angular-2-applications/canactivate-and-canactivatechild

Here is my code that I used in my project! 这是我在项目中使用的代码!

In-app module file I used auth guard like this : 应用内模块文件我使用了auth guard这样:

const ROUTES = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'register', component: RegisterComponent },
  { path: 'home', component: HomeComponent },
  { path: 'books', component: BookComponent,canActivate:[AuthGuardService] },
  { path: 'book-details/:id', component: BookDetailComponent,canActivate:[AuthGuardService] },
  { path: 'book-create', component: BookCreateComponent,canActivate:[AuthGuardService] },
];

And here is my auth guard service : 这是我的授权服务:

import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';


@Injectable()
export class AuthGuardService implements CanActivate {

    constructor( public router: Router) { }

    canActivate(): boolean {

        if (sessionStorage.getItem('id') == null) {
            this.router.navigate(['home']);
            return false;
        }
        return true;
    }
}

This is how you can implement auth guard in your code. 这是您在代码中实现auth guard的方法。

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

相关问题 如果用户登录,Angular 2 重定向 - Angular 2 redirect if user is logged in Angular - 如何根据用户角色重定向到仪表板 - Angular - How to redirect to Dashboard based on user role 如果已记录用户,则角度登录组件重定向 - Angular Login Component Redirect if User is Already logged 用户以角度登录后显示侧边栏和仪表板组件 - Show Sidebar And Dashboard Component after user gets logged In in angular 如何根据 Angular 中的用户角色将登录重定向到仪表板 - How to redirect login to dashboard based on user role in Angular 如果用户未登录Angular2 2.0.0-rc.4,则重定向到登录路由 - Redirect to login route if the user not logged in Angular2 2.0.0-rc.4 如果用户未登录,如何重定向到 Angular 自定义登录页面 - How to redirect to an Angular custom made Login page if the user is not logged in Angular 2路由器-注销后,用户尝试导航到登录页面时,我需要app.ts进行重定向 - Angular 2 router - When logged out and a user tries to navigate to a logged in page I need the app.ts to redirect 用户登录 Angular 时如何重定向到不同的主页? - How do I redirect to different home page when user is logged in Angular? AspNetBoilerplate(免费版+ .NET Core + Angular):如果用户已经登录,则从登录页面重定向到首页 - AspNetBoilerplate (Free version + .NET Core + Angular): redirect to home from login page if user is already logged in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM