嗨,我在后端使用Django,在前端使用Angular,尝试登录时收到无效令牌错误:指定的令牌无效:无法读取未定义的属性“ replace”,您知道这是什么问题??
这是user.service.ts的主音
import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaders, HttpInterceptor, HttpRequest, HttpHandler, HttpEvent} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { CanActivate,Router } from '@angular/router'
import { tap, shareReplay } from 'rxjs/operators';
import * as jwtDecode from 'jwt-decode';
import * as moment from 'moment';
import { environment } from '../environments/environment';
@Injectable({
providedIn: 'root'
})
export class UserService {
constructor(private http:HttpClient,
private _router: Router) { }
private setSession(authResult) {
const token = authResult.token;
const payload = <JWTPayload> jwtDecode(token);
const expiresAt = moment.unix(payload.exp);
localStorage.setItem('token', authResult.token);
localStorage.setItem('expires_at', JSON.stringify(expiresAt.valueOf()));
}
loginUser(userData):Observable<any>{
return this.http.post('http://127.0.0.1:8000/MyProjects/auth/',
userData
).pipe(
tap(response => this.setSession(response)),
shareReplay(),
);
}
}
interface JWTPayload {
user_id: number;
username: string;
email: string;
exp: number;
}