Last active
August 29, 2022 14:33
-
-
Save KolbySisk/55c1dbc00c891c36246fb4771136ac0f to your computer and use it in GitHub Desktop.
This file contains 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 { Middleware } from 'next-api-route-middleware'; | |
import { getUserByCookie } from '../utils'; | |
export type User = { userId: string }; | |
export type NextApiRequestWithUser = NextApiRequest & User; | |
export const addUser: Middleware<NextApiRequestWithUser> = async (req, res, next) => { | |
const authCookie = await getUserByCookie(); | |
if (authCookie) { | |
req.userId = authCookie.userId; | |
next(); | |
} else { | |
res.status(401).send({ message: 'Invalid auth cookie.' }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment