我尝试在Angular客户端(MEAN堆栈)中删除Node API的cookie,但是它不起作用。
我在Node端有一个控制器和一个路由器,在Angular上我订阅了该服务。
//路由器节点
authRouter.get('/logout', this.passport.authenticate('jwt', {
session: false
}), (req, res) => {
logout()
.then(apiResponse => sendApiSuccessResponse(res, Vocabulary.request.success, apiResponse))
.catch(apiResponse => sendApiErrorResponse(res, Vocabulary.request.error, apiResponse));
});
//控制器节点
const logout = (res) => {
res.cookie(process.env.COOKIE_NAME, '', {
maxAge: -1,
httpOnly: true
});
};
//服务角度
public logout(): Observable < any > {
// GET '/auth/logout'
console.log('logout service ok');
return this.HttpClient.get(`${environment.apiUrl}/auth/logout`)
}
//组件角度呼叫注销
public logout = () => {
return this.AuthService.logout()
};
ngOnInit() {
this.logout();
};
您认为代码还可以吗?
答案 0 :(得分:0)
尝试cookies.set('process.env.COOKIE_NAME', {maxAge: Date.now()});
答案 1 :(得分:0)
maxAge: 0
设置为0表示立即删除此Cookie。