2024-06-12 02:12:12 +00:00
|
|
|
import { generateIdFromEntropySize, type Session, type User } from 'lucia';
|
2024-04-25 18:26:05 +00:00
|
|
|
import { TimeSpan, createDate } from 'oslo';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
2024-05-08 00:19:13 +00:00
|
|
|
import db from '../../db';
|
|
|
|
|
import { password_reset_tokens } from '$db/schema';
|
2024-03-02 02:00:27 +00:00
|
|
|
|
|
|
|
|
export async function createPasswordResetToken(userId: string): Promise<string> {
|
|
|
|
|
// optionally invalidate all existing tokens
|
|
|
|
|
await db.delete(password_reset_tokens).where(eq(password_reset_tokens.user_id, userId));
|
2024-04-25 18:26:05 +00:00
|
|
|
const tokenId = generateIdFromEntropySize(40);
|
|
|
|
|
await db.insert(password_reset_tokens).values({
|
|
|
|
|
id: tokenId,
|
|
|
|
|
user_id: userId,
|
|
|
|
|
expires_at: createDate(new TimeSpan(2, 'h')),
|
|
|
|
|
});
|
2024-03-02 02:00:27 +00:00
|
|
|
return tokenId;
|
2024-04-25 18:26:05 +00:00
|
|
|
}
|
2024-06-12 02:12:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the user is fully authenticated.
|
|
|
|
|
*
|
|
|
|
|
* @param user - The user object.
|
|
|
|
|
* @param session - The session object.
|
|
|
|
|
* @returns True if the user is fully authenticated, otherwise false.
|
|
|
|
|
*/
|
|
|
|
|
export function userFullyAuthenticated(user: User | null, session: Session | null) {
|
|
|
|
|
return user && session && (!session.isTwoFactorAuthEnabled || session.isTwoFactorAuthenticated);
|
|
|
|
|
}
|