7 |
8 |
Secret
9 |
10 |
11 | You can get source code here: https://github.com/Longfld/ASPNETcoreAngularJWT
12 |
13 |
14 |
Hi {{userName}}
15 |
16 |
17 | `
18 | })
19 | export class SecretComponent implements OnInit {
20 |
21 | userName: string;
22 |
23 | constructor(private authService: AuthService) { }
24 |
25 | ngOnInit() {
26 | this.authService.getUserInfo$().subscribe(
27 | res => {
28 | if (res != null && res.data) {
29 | let thisuser = res.data
30 | if (thisuser && thisuser.userName) {
31 | this.userName = thisuser.userName;
32 | }
33 | }
34 | });
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/wwwroot/app/shared/AuthBearerInterface.ts:
--------------------------------------------------------------------------------
1 | export interface AuthBearer{
2 | state : number;
3 | msg : string;
4 | data : any;
5 | }
--------------------------------------------------------------------------------
/wwwroot/app/shared/auth.service.ts:
--------------------------------------------------------------------------------
1 | import { Injectable } from "@angular/core";
2 |
3 | import { HttpClient, HttpHeaders, HttpInterceptor, HttpRequest, HttpResponse, HttpHandler, HttpErrorResponse, HttpEvent } from "@angular/common/http";
4 | import { Router, CanActivate } from '@angular/router';
5 |
6 | import { Observable, of } from 'rxjs';
7 | import { tap, map, distinctUntilChanged, debounceTime, catchError } from 'rxjs/operators'
8 |
9 | import { AuthBearer } from './AuthBearerInterface';
10 |
11 | @Injectable()
12 | export class AuthService implements CanActivate, HttpInterceptor {
13 | private tokeyKey = "token";
14 |
15 | constructor(private http: HttpClient, private router: Router) { }
16 |
17 | intercept(request: HttpRequest