Files
formbricks/apps/web/lib/jwt.ts
Dhruwang Jariwala b00beadf2e Add Email Verification option to Link Surveys (#762)
* completed frontend

* Adds email verifaction for Link Surveys

* remove console.log

* run pnpm format

* rename userId to verify

* add loading state

* fix type names

* add types to prisma

---------

Co-authored-by: Dhruwang Jariwala <dhruwang@Dhruwangs-MacBook-Pro.local>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2023-09-02 15:34:00 +09:00

58 lines
1.5 KiB
TypeScript

import jwt from "jsonwebtoken";
import { prisma } from "@formbricks/database";
import { env } from "@/env.mjs";
export function createToken(userId, userEmail, options = {}) {
return jwt.sign({ id: userId }, env.NEXTAUTH_SECRET + userEmail, options);
}
export function createTokenForLinkSurvey(surveyId, userEmail) {
return jwt.sign({ email: userEmail }, env.NEXTAUTH_SECRET + surveyId);
}
export function verifyTokenForLinkSurvey(token, surveyId): Promise<boolean> {
return new Promise((resolve) => {
jwt.verify(token, env.NEXTAUTH_SECRET + surveyId, function (err) {
if (err) {
resolve(false);
} else {
resolve(true);
}
});
});
}
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, env.NEXTAUTH_SECRET + userEmail);
}
export const createInviteToken = (inviteId: string, email: string, options = {}) => {
return jwt.sign({ inviteId, email }, env.NEXTAUTH_SECRET, options);
};
export const verifyInviteToken = async (token: string) => {
try {
const decoded = jwt.decode(token);
return {
inviteId: decoded.inviteId,
email: decoded.email,
};
} catch (error) {
console.error("Error verifying invite token:", error);
throw new Error("Invalid or expired invite token");
}
};