簡體   English   中英

Angular 2/4:如果用戶已經登錄,如何限制對登錄路由的訪問?

[英]Angular 2/4: How to restrict access to Login Route if user is already logged in?

我有以下路線定義。

export const Routes = RouterModule.forChild([
    {
        path: 'login',
        component: LoginComponent
    },
    {
        path: 'protected',
        canActivate: [AuthGuardService],
        component: ProtectedComponent
    },
    {
        path: 'home',
        component: HomeComponent,
        canActivate: [AuthGuardService],
    },
]);

我已成功實現AuthGuardService ,如果用戶AuthGuardService ,則限制對受保護路由的訪問。

我想要實現的是,如果用戶已經登錄並訪問了登錄路由,我希望它重定向到主頁等其他路由。

如果我是你,我可能會簡單地實現另一個與您的AuthGuardService完全相反的GuardService - 只有在本地存儲中沒有會話令牌的情況下才允許用戶。 然后使用它來保護登錄組件。

export const Routes = RouterModule.forChild([
    {
        path: 'login',
        canActivate: [AnonymousGuardService],
        component: LoginComponent
    },
    {
        path: 'protected',
        canActivate: [AuthGuardService],
        component: ProtectedComponent
    },
    {
        path: 'home',
        component: HomeComponent,
        canActivate: [AuthGuardService],
    },
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM