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;
|
|
|
|
};
|
|
|
|
interface Session extends DefaultSession {
|
|
|
|
user?: CalendsoSessionUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getSession(options: GetSessionOptions): Promise<CalendsoSession | null> {
|
|
|
|
const session = await getSessionInner(options);
|
|
|
|
|
|
|
|
// that these are equal are ensured in `[...nextauth]`'s callback
|
|
|
|
return session as CalendsoSession;
|
|
|
|
}
|