Files
formbricks/apps/web/lib/jwt.ts
Matti Nannt 6cd3878700 make all env variables required at build time #110 (#111)
* make all env variables required at build time

* update env files

* add .env.docker file with basic docker configuration

* update Readme with new instructions
2022-10-16 11:10:20 +02:00

25 lines
577 B
TypeScript

import jwt from "jsonwebtoken";
import { prisma } from "database";
export function createToken(userId, userEmail, options = {}) {
return jwt.sign({ id: userId }, process.env.NEXTAUTH_SECRET + userEmail, options);
}
export async function verifyToken(token, userEmail = "") {
if (!userEmail) {
const { id } = jwt.decode(token);
const foundUser = await prisma.user.findUnique({
where: { id },
});
if (!foundUser) {
return null;
}
userEmail = foundUser.email;
}
return jwt.verify(token, process.env.NEXTAUTH_SECRET + userEmail);
}