2022-11-09 22:02:06 -05:00
|
|
|
import { USER_COOKIE_NAME, TOKEN_COOKIE_NAME } from "@lib/constants"
|
2022-11-11 22:17:44 -05:00
|
|
|
import { User } from "@lib/server/prisma"
|
2022-11-09 22:02:06 -05:00
|
|
|
import { setCookie } from "cookies-next"
|
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
import { generateAndExpireAccessToken } from "./generate-access-token"
|
|
|
|
|
|
|
|
export const signin = async (
|
|
|
|
userId: User["id"],
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse
|
|
|
|
) => {
|
|
|
|
const token = await generateAndExpireAccessToken(userId)
|
|
|
|
setCookie(USER_COOKIE_NAME, userId, {
|
|
|
|
maxAge: 30 * 24 * 60 * 60, // 30 days,
|
|
|
|
req,
|
|
|
|
res
|
|
|
|
})
|
|
|
|
setCookie(TOKEN_COOKIE_NAME, token, {
|
|
|
|
maxAge: 30 * 24 * 60 * 60, // 30 days
|
|
|
|
req,
|
|
|
|
res
|
|
|
|
})
|
|
|
|
|
|
|
|
return token
|
|
|
|
}
|