I need to remove the refresh token from the server when the user logs out.
auth.service.ts
deleteToken(refreshToken:any){
return this.http.delete(`${environment.baseUrl}/logout`, refreshToken).toPromise()
}
header.component.ts
refreshToken = localStorage.getItem('refreshToken')
logout = async()=>{
try {
console.log(this.refreshToken)
await this.authService.deleteToken({refreshToken:this.refreshToken})
localStorage.removeItem("refreshToken");
localStorage.removeItem("token");
this.router.navigateByUrl('/login');
} catch (error) {
console.log(error)
}
}
The console.log(this.refreshToken) displays the token retrieved from local storage correctly.
Backend authController.js
exports.deleteToken = async (req, res) => {
try {
let user = await User.findOne({ refreshToken: req.body.refreshToken })
console.log(req.body.refreshToken)
if (req.body.refreshToken == user.refreshToken) {
await user.updateOne({ $unset: { refreshToken: "" } });
res.sendStatus(200);
} else {
res.status(400).json({ message: "Error while deleting token" })
}
} catch (error) {
console.log(error)
}
}
When I output req.body.refreshToken using console.log, it appears as undefined.