2021-08-18 11:52:25 +00:00
|
|
|
import { compare, hash } from "bcryptjs";
|
|
|
|
import { DefaultSession } from "next-auth";
|
|
|
|
import { getSession as getSessionInner, GetSessionOptions } from "next-auth/client";
|
2021-03-24 15:03:04 +00:00
|
|
|
|
2021-08-18 11:52:25 +00:00
|
|
|
export async function hashPassword(password: string) {
|
|
|
|
const hashedPassword = await hash(password, 12);
|
|
|
|
return hashedPassword;
|
2021-03-24 15:03:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 11:52:25 +00:00
|
|
|
export async function verifyPassword(password: string, hashedPassword: string) {
|
|
|
|
const isValid = await compare(password, hashedPassword);
|
|
|
|
return isValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultSessionUser = NonNullable<DefaultSession["user"]>;
|
|
|
|
type CalendsoSessionUser = DefaultSessionUser & {
|
|
|
|
id: number;
|
|
|
|
username: string;
|
|
|
|
};
|
2021-08-18 12:15:22 +00:00
|
|
|
export interface Session extends DefaultSession {
|
2021-08-18 11:52:25 +00:00
|
|
|
user?: CalendsoSessionUser;
|
|
|
|
}
|
|
|
|
|
2021-08-18 12:15:22 +00:00
|
|
|
export async function getSession(options: GetSessionOptions): Promise<Session | null> {
|
2021-08-18 11:52:25 +00:00
|
|
|
const session = await getSessionInner(options);
|
|
|
|
|
|
|
|
// that these are equal are ensured in `[...nextauth]`'s callback
|
2021-08-18 12:15:22 +00:00
|
|
|
return session as Session | null;
|
2021-08-18 11:52:25 +00:00
|
|
|
}
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
export enum ErrorCode {
|
|
|
|
UserNotFound = "user-not-found",
|
|
|
|
IncorrectPassword = "incorrect-password",
|
|
|
|
UserMissingPassword = "missing-password",
|
|
|
|
TwoFactorDisabled = "two-factor-disabled",
|
|
|
|
TwoFactorAlreadyEnabled = "two-factor-already-enabled",
|
|
|
|
TwoFactorSetupRequired = "two-factor-setup-required",
|
|
|
|
SecondFactorRequired = "second-factor-required",
|
|
|
|
IncorrectTwoFactorCode = "incorrect-two-factor-code",
|
|
|
|
InternalServerError = "internal-server-error",
|
|
|
|
NewPasswordMatchesOld = "new-password-matches-old",
|
|
|
|
}
|