import { IronSessionOptions, sealData, unsealData } from "iron-session"; import { withIronSessionApiRoute, withIronSessionSsr } from "iron-session/next"; import { GetServerSideProps, NextApiHandler, NextApiRequest } from "next"; import { prisma } from "../db"; import { randomid } from "./nanoid"; const sessionOptions: IronSessionOptions = { password: process.env.SECRET_PASSWORD, cookieName: "rallly-session", cookieOptions: { secure: process.env.NODE_ENV === "production", }, ttl: 0, // basically forever }; export function withSessionRoute(handler: NextApiHandler) { return withIronSessionApiRoute(handler, sessionOptions); } export function withSessionSsr(handler: GetServerSideProps) { return withIronSessionSsr(handler, sessionOptions); } export const decryptToken = async
>( token: string, ): Promise
=> {
return await unsealData(token, { password: sessionOptions.password });
};
export const createToken = async