Я новичок в angular, и я пытаюсь проверить свой токен аутентификации с помощью angular-jwt на Angular 6. Целью проверки токена будет разрешить разные кнопки, когда пользователь входит в систему, и показывать другой набор кнопок, когда они выходят из системы.
Это моя служба авторизации.
import { JwtHelperService } from '@auth0/angular-jwt';
constructor(private http:HttpClient, public jwtHelper:JwtHelperService)
{}
loggedIn()
{
console.log(this.jwtHelper.isTokenExpired(this.authtoken));
}
И это немного моего HTML-кода
<a *ngIf="!authService.loggedIn()" [routerLink]="['/login']"><button class.....
<a *ngIf="!authService.loggedIn()" [routerLink]="['/register']"><button class.....
<a *ngIf="authService.loggedIn()" [routerLink]="['/profile']"><button class....
<a *ngIf="authService.loggedIn()" [routerLink]="['/profile']"><button class.....
Теперь моя проблема заключается в том, что до того, как я войду в систему, он правильно регистрируется в консоли как истина, но после того, как я войду в систему и перейду на страницу профиля, кнопки не изменятся, потому что он все еще регистрирует истину, а затем снова регистрирует ложь.
Я думаю, это связано с использованием функции получения токена в модуле приложения, но я не уверен, как еще ее реализовать.
Компонент модуля моего приложения:
....
imports: [BrowserModule,
[JwtModule.forRoot({
config: {tokenGetter:tokenGetter,whitelistedDomains['localhost:3000']}
})]
providers: [AuthService,JwtHelperService]
})
export function tokenGetter() {
return localStorage.getItem('access_token');
}