Created
April 24, 2022 22:05
-
-
Save atakde/cb6cd2ffcbac6c0a2dff80ff197dc47c to your computer and use it in GitHub Desktop.
Node.js JWT Logout With HTTP Only Cookie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serialize } from 'cookie'; | |
export default function logout(req, res) { | |
const { cookies } = req; | |
const jwt = cookies.token; | |
if (!jwt) { | |
return res.status(401).json({ | |
status: 'error', | |
error: 'Unauthorized', | |
}); | |
} | |
const serialized = serialize('token', null, { | |
httpOnly: true, | |
secure: process.env.NODE_ENV === 'production', | |
sameSite: 'strict', | |
maxAge: -1, | |
path: '/', | |
}); | |
res.setHeader('Set-Cookie', serialized); | |
res.status(200).json({ | |
status: 'success', | |
message: 'Logged out', | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment