java回调警局,使用重定向,回调和未知路径处理警卫

对于一个项目,我有一个路由器,其中包含以下可用路径:

const appRoutes: Routes = [

{path: '', component: AppComponent},

{path: 'dashboard', component: DashboardComponent},

{path: 'leaderboard', component: LeaderboardComponent},

{path: 'authentication', component: AuthenticationComponent},

{path: '**', redirectTo: '/authentication', pathMatch: 'full}

];

AuthenticationComponent的Injectable Service正在处理路由器 . 如果没有身份验证,用户将被重定向到/身份验证,无论路由是什么,如果他已登录,则重定向到/ dashboard .

问题是如果我想重新加载/排行榜页面,它每次都会重定向到/ dashboard,它也不应该是身份验证服务的工作 .

我已经尝试过,使用This guide来理解警卫,这使我能够通过/仪表板和/排行榜处理基本导航,Auth0的回调和刷新,但是当我已经认证时访问我的登录页面时,它不是重定向,与未知路径相同的行为 .

有没有办法让我检查我的路由器是否知道所提供的路由,并且如果用户已登录则正确重定向?

我的卫兵:

import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';

import {AuthenticationService} from './component/authentification/authentication.service';

import {Injectable} from '@angular/core';

@Injectable()

export class AuthGuard implements CanActivate {

constructor(private authService: AuthenticationService,

private router: Router) {

}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {

console.log(route, state);

this.authService.handleAuthentication();

if (this.authService.isAuthenticated()) {

return (true);

} else {

this.router.navigate(['/authentication']);

}

}

}

我目前的路由器:

import {NgModule} from '@angular/core';

import {RouterModule, Routes} from '@angular/router';

import {DashboardComponent} from './component/dashboard/dashboard.component';

import {LeaderboardComponent} from './component/leaderboard/leaderboard.component';

import {AuthenticationComponent} from './component/authentification/authentication.component';

import {AppComponent} from './app.component';

import {AuthGuard} from "./app-routing.guard";

const appRoutes: Routes = [

{path: '', canActivate: [AuthGuard], redirectTo: '/dashboard', pathMatch: 'full'},

{path: 'dashboard', canActivate: [AuthGuard], component: DashboardComponent},

{path: 'leaderboard', canActivate: [AuthGuard], component: LeaderboardComponent},

{path: 'authentication', component: AuthenticationComponent},

{path: '**', canActivate: [AuthGuard], redirectTo: '/authentication'}

];

@NgModule({

imports: [

RouterModule.forRoot(

appRoutes

)

],

exports: [

RouterModule

]

})

export class AppRoutingModule {

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值