我是角色6的初学者。我对canActivate有一些疑问,允许用户访问授权用户的URL: -
以下代码: -
app.routing.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from 'src/app/app.component';
import { DashboardComponent } from 'src/app/dashboard/dashboard.component';
import { AuthGuard } from 'src/app/guards/auth.guard';
const routes: Routes = [
{ path: 'home', component: AppComponent },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }
];
@NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ],
declarations: []
})
export class AppRoutingModule { }
auth.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { TokenvalidityService } from '../services/tokenvalidity.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private _tokenvalidityService: TokenvalidityService) { }
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
console.log("Auth guard "+localStorage.getItem('currentUser'))
console.log(this._tokenvalidityService.checkTokenExpired());
if (!this._tokenvalidityService.checkTokenExpired()) {
console.log("Inside Auth guard if");
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/home'], { queryParams: { returnUrl: state.url }});
return false;
}
}
现在,如果尝试为经过身份验证的用户点击网址(http://localhost:4200/dashboard),则会重定向到主页(http://localhost:4200)。但我可以在本地存储中看到登录的用户详细信息。
你能帮我解决这个问题。我无法跟踪此重定向无法正常工作的原因。
package.json文件: -
{
"name": "angular-test-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^6.0.3",
"@angular/common": "^6.0.3",
"@angular/compiler": "^6.0.3",
"@angular/core": "^6.0.3",
"@angular/forms": "^6.0.3",
"@angular/http": "^6.0.3",
"@angular/platform-browser": "^6.0.3",
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/router": "^6.0.3",
"@auth0/angular-jwt": "^2.0.0",
"@types/jwt-decode": "^2.2.1",
"core-js": "^2.5.4",
"jwt-decode": "^2.2.0",
"rxjs": "^6.0.0",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular/compiler-cli": "^6.0.3",
"@angular-devkit/build-angular": "~0.6.8",
"typescript": "~2.7.2",
"@angular/cli": "~6.0.8",
"@angular/language-service": "^6.0.3",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "~4.2.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.0",
"karma-jasmine": "~1.1.1",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.3.0",
"ts-node": "~5.0.1",
"tslint": "~5.9.1"
},
"description": "This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 6.0.8.",
"main": "index.js",
"author": "",
"license": "ISC"
}
答案 0 :(得分:0)
您没有提及日志的输出。您是否尝试过调试此位[ A-Za-z0-9@#.-]{1,149}$
。