Of course! Here’s the question with additional details:
“I want when I press logout, the token in localStorage to be cleared or any necessary action to properly log out without encountering errors. I’ve implemented a logout function that attempts to clear the token from localStorage and makes an API call to log out the user. However, after refreshing the page, I encounter an error from the console like this:”
InvalidTokenError: Invalid token specified: must be a string
index.js:36 Uncaught InvalidTokenError: Invalid token specified: must be a string
//navbar.js
const Logout = async () => {
try {
await axios.delete('https://api.poultrypal.my.id/logout');
window.location.reload();
} catch (error) {
console.log(error);
}
};
//controller/users.js
export const Logout = async (req, res) => {
try {
const refreshToken = req.cookies.refreshToken;
if (!refreshToken) {
return res.sendStatus(204);
}
const user = await Users.findOne({ where: { refresh_token: refreshToken } });
if (!user) {
return res.sendStatus(204);
}
await Users.update({ refresh_token: null }, { where: { id: user.id } });
res.clearCookie('refreshToken');
return res.sendStatus(200);
} catch (error) {
console.log(error);
return res.status(500).send(error.message);
}
};
New contributor
Hansen Dharma Tangtobing Tjoa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.