Files
formbricks/lib/auth.ts
Matthias Nannt 803007e0e2 Feature/fix authentication issues (#15)
* fix errors when not authenticated
* update syntax highlighting and react code sample
2022-08-09 06:36:39 +09:00

31 lines
803 B
TypeScript

import { compare, hash } from "bcryptjs";
export async function hashPassword(password: string) {
const hashedPassword = await hash(password, 12);
return hashedPassword;
}
export async function verifyPassword(password: string, hashedPassword: string) {
const isValid = await compare(password, hashedPassword);
return isValid;
}
export function requireAuthentication(gssp) {
return async (context) => {
const { req, resolvedUrl } = context;
const token = req.cookies.userToken;
if (!token) {
return {
redirect: {
destination: `/auth/signin?callbackUrl=${encodeURIComponent(
resolvedUrl
)}`,
statusCode: 302,
},
};
}
return await gssp(context); // Continue on to call `getServerSideProps` logic
};
}