Files
sign/packages/lib/server/getUserFromToken.ts

28 lines
752 B
TypeScript
Raw Normal View History

2023-06-09 03:55:30 +00:00
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
import { NextRequest } from "next/server";
2023-01-20 10:23:40 +01:00
import prisma from "@documenso/prisma";
import { User as PrismaUser } from "@prisma/client";
import { getToken } from "next-auth/jwt";
export async function getUserFromToken(
2023-06-09 03:55:30 +00:00
req: GetServerSidePropsContext["req"] | NextRequest | NextApiRequest,
res?: NextApiResponse // TODO: Remove this optional parameter
2023-01-20 10:23:40 +01:00
): Promise<PrismaUser | null> {
const token = await getToken({ req });
const tokenEmail = token?.email?.toString();
2023-06-09 03:55:30 +00:00
if (!token || !tokenEmail) {
2023-01-20 10:23:40 +01:00
return null;
}
const user = await prisma.user.findFirst({
2023-01-20 10:23:40 +01:00
where: { email: tokenEmail },
});
if (!user) {
return null;
}
return user;
2023-01-20 10:23:40 +01:00
}