Skip to content

Instantly share code, notes, and snippets.

@KolbySisk
Last active August 29, 2022 14:33
Show Gist options
  • Save KolbySisk/55c1dbc00c891c36246fb4771136ac0f to your computer and use it in GitHub Desktop.
Save KolbySisk/55c1dbc00c891c36246fb4771136ac0f to your computer and use it in GitHub Desktop.
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