2022-02-15 20:30:52 +00:00
|
|
|
import { compare, hash } from "bcryptjs";
|
2022-08-09 09:21:15 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-08-03 19:18:26 +00:00
|
|
|
import type { Session } from "next-auth";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { getSession as getSessionInner, GetSessionParams } from "next-auth/react";
|
2022-02-15 20:30:52 +00:00
|
|
|
|
2022-08-09 09:21:15 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
|
2022-02-15 20:30:52 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
export function validPassword(password: string) {
|
|
|
|
if (password.length < 7) return false;
|
|
|
|
|
|
|
|
if (!/[A-Z]/.test(password) || !/[a-z]/.test(password)) return false;
|
|
|
|
|
|
|
|
if (!/\d+/.test(password)) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-22 17:27:06 +00:00
|
|
|
export async function getSession(options: GetSessionParams): Promise<Session | null> {
|
|
|
|
const session = await getSessionInner(options);
|
|
|
|
|
|
|
|
// that these are equal are ensured in `[...nextauth]`'s callback
|
|
|
|
return session as Session | null;
|
|
|
|
}
|
2022-07-27 23:28:21 +00:00
|
|
|
|
2022-08-02 05:52:25 +00:00
|
|
|
export function isPasswordValid(password: string): boolean;
|
|
|
|
export function isPasswordValid(
|
|
|
|
password: string,
|
|
|
|
breakdown: boolean
|
|
|
|
): { caplow: boolean; num: boolean; min: boolean };
|
|
|
|
export function isPasswordValid(password: string, breakdown?: boolean) {
|
2022-07-29 13:23:54 +00:00
|
|
|
let cap = false, // Has uppercase characters
|
|
|
|
low = false, // Has lowercase characters
|
|
|
|
num = false, // At least one number
|
|
|
|
min = false; // Seven characters
|
2022-07-27 23:28:21 +00:00
|
|
|
if (password.length > 6) min = true;
|
|
|
|
for (let i = 0; i < password.length; i++) {
|
|
|
|
if (!isNaN(parseInt(password[i]))) num = true;
|
|
|
|
else {
|
|
|
|
if (password[i] === password[i].toUpperCase()) cap = true;
|
|
|
|
if (password[i] === password[i].toLowerCase()) low = true;
|
|
|
|
}
|
|
|
|
}
|
2022-07-29 13:23:54 +00:00
|
|
|
return !!breakdown ? { caplow: cap && low, num, min } : cap && low && num && min;
|
2022-08-02 05:52:25 +00:00
|
|
|
}
|
2022-08-09 09:21:15 +00:00
|
|
|
|
|
|
|
type CtxOrReq = { req: NextApiRequest; ctx?: never } | { ctx: { req: NextApiRequest }; req?: never };
|
|
|
|
|
|
|
|
export const ensureSession = async (ctxOrReq: CtxOrReq) => {
|
|
|
|
const session = await getSession(ctxOrReq);
|
|
|
|
if (!session?.user.id) throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
|
|
|
return session;
|
|
|
|
};
|