我正在尝试使用 jwt-passport 和 nestjs 来设置一个非常简单的登录系统。我遵循了本教程:https : //docs.nestjs.com/techniques/authentication但我无法让它工作。我对这些东西真的很陌生,如果有人能给我指路,我将不胜感激。
我将登录名发送到服务器的方式:
this.clientAuthService.login(this.userName, this.password).then(response => {
this.clientAuthService.setToken(response.access_token);
this.router.navigate(['/backend']);
});
Run Code Online (Sandbox Code Playgroud)
我的 ClientAuthService:
export class ClientAuthService {
constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId) {
}
getToken(): string {
if (isPlatformBrowser(this.platformId)) {
return localStorage.getItem(TOKEN_NAME);
} else {
return '';
}
}
setToken(token: string): void {
if (isPlatformBrowser(this.platformId)) {
localStorage.setItem(TOKEN_NAME, token);
}
}
removeToken() {
if (isPlatformBrowser(this.platformId)) {
localStorage.removeItem(TOKEN_NAME);
}
}
getTokenExpirationDate(token: string): Date {
const decoded = jwt_decode(token);
if (decoded.exp === undefined) {
return null; …Run Code Online (Sandbox Code Playgroud)