我非常喜欢新的nodejs
和angular 7
我使用jwt token
进行身份验证,我想在令牌过期后自动重定向到登录页面。我知道类似的问题已经被问过了,但我也很累,因为我没有为我工作。
管理控制器.js
const controller = require("./admin.service");
const jwt = require("jsonwebtoken")
module.exports = {
verifyAdmin: (req, res) => {
const sign = jwt.sign({admin_user: req.body}, "mysecretkey", {
expiresIn: "1h"
})
req.body.admin_token = sign
const body = req.body;
controller.adminLogin(body, (err, result) => {
if(err) {
console.log(err)
res.status(500).json({
success: 0,
message: "Database connection error"
})
} else{
if(result[0].length > 0) {
console.log(result[0][0].admin_user)
res.json({
success: 1,
message: result[0],
token: sign
})
} else {
res.json({
success:0,
message: "We cannot find it"
})
}
}
})
}
所以有人建议使用HttpInterceptor
是个好主意,我也用了,但不是wokring。
验证服务.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders, HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http';
import { Teacher } from '../shared/teacher.model';
import { Subject, Observable } from 'rxjs';
@Injectable()
export class AuthService implements HttpInterceptor {
// private adminValidateUrl = "http://localhost:3000/getData";
private adminValidateUrl = "http://localhost:3000/adminApi/verifyAdmin"
private verifyAdminToken = "http://localhost:3000/adminApi/getAdminUserName"
private getTeacherRecordsUrl = "http://localhost:3000/api/getTeacherRecords"
private removeTeacherUrl = "http://localhost:3000/adminApi/removeTeacherRecord"
subject = new Subject<Teacher[]>();
teachers: Teacher[] = []
constructor(private http: HttpClient) { }
headers = new Headers({
'Content-Type': 'application/json',
'Token': localStorage.getItem("admin_token")
});
adminValidation(adminData: any) {
console.log(adminData)
return this.http.post<any>(this.adminValidateUrl, adminData)
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authReq = req.clone({ headers: req.headers.set("Token", localStorage.getItem("Token")) });
console.log("Sending request with new header now ...");
//send the newly created request
return next.handle(authReq)
.pipe(err => {
// onError
console.log(err);
if (err instanceof HttpErrorResponse) {
console.log(err.status);
console.log(err.statusText);
if (err.status === 401) {
window.location.href = "/login";
}
}
return Observable.throw(err);
}) as any;
}
getAdminUserName() {
const token = localStorage.getItem('admin_token');
return this.http.get<any>(this.verifyAdminToken, {
observe: "body",
headers: new HttpHeaders().set("Authorization", "Bearer " + token)
});
}
getTeacherRecordsFromDB() {
return this.http.get<any>(this.getTeacherRecordsUrl, {
observe: "body"
})
}
removeTeacher(teacher: Teacher) {
const token = localStorage.getItem('admin_token');
return this.http.post<any>(this.removeTeacherUrl, teacher, {
observe: "body",
headers: new HttpHeaders().set("Authorization", "Bearer " + token)
})
}
}
也可能是我没有正确使用它。
因此,我想一种方法,我我角页面自动重定向到登录页面时,令牌到期与一些消息令牌到期。
- 谢谢-谢谢
1条答案
按热度按时间0yg35tkg1#
您可以在拦截器中使用以下代码,在令牌过期时将页面重定向到登录