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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 11:24:32 +01:00
|
|
|
const user = await prisma.user.findFirst({
|
2023-01-20 10:23:40 +01:00
|
|
|
where: { email: tokenEmail },
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-09 11:57:33 +01:00
|
|
|
if (!user) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 11:24:32 +01:00
|
|
|
return user;
|
2023-01-20 10:23:40 +01:00
|
|
|
}
|