Skip to content

Instantly share code, notes, and snippets.

@Ethaan
Created March 1, 2021 17:51
Show Gist options
  • Save Ethaan/bbcce08106e5e6c68f6fe10f69bc4468 to your computer and use it in GitHub Desktop.
Save Ethaan/bbcce08106e5e6c68f6fe10f69bc4468 to your computer and use it in GitHub Desktop.
Login user on NextJS using pages/api
import cookie from 'cookie';
// types
import type { NextApiResponse, NextApiRequest } from 'next';
// mocks
import { mockUser } from 'mocks/user';
const handler = (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'POST') {
res.setHeader(
'Set-Cookie',
cookie.serialize('token', '123456', {
httpOnly: true,
})
);
return res.status(200).json(mockUser());
} else {
return res.status(500).send('Weirdo');
}
};
export default handler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment