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-08-15 23:25:41 +00:00
|
|
|
import { db } from './api/infrastructure/database/index';
|
|
|
|
|
import { password_reset_tokens } from './api/infrastructure/database/tables';
|
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
|
|
|
|
|
|
|
|
/**
|
2024-06-15 02:11:18 +00:00
|
|
|
* Checks if the user is not fully authenticated.
|
2024-06-12 02:12:12 +00:00
|
|
|
*
|
|
|
|
|
* @param user - The user object.
|
|
|
|
|
* @param session - The session object.
|
2024-06-15 02:11:18 +00:00
|
|
|
* @returns True if the user is not fully authenticated, otherwise false.
|
2024-06-12 02:12:12 +00:00
|
|
|
*/
|
2024-06-15 02:11:18 +00:00
|
|
|
export function userNotFullyAuthenticated(user: User | null, session: Session | null) {
|
2024-06-17 20:06:45 +00:00
|
|
|
return user && session && session.isTwoFactorAuthEnabled && !session.isTwoFactorAuthenticated;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 06:12:36 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if the user is not fully authenticated.
|
|
|
|
|
*
|
|
|
|
|
* @param {User | null} user - The user object.
|
|
|
|
|
* @param {Session | null} session - The session object.
|
|
|
|
|
* @returns {boolean} True if the user is not fully authenticated, otherwise false.
|
|
|
|
|
*/
|
2024-06-17 20:06:45 +00:00
|
|
|
export function userNotAuthenticated(user: User | null, session: Session | null) {
|
|
|
|
|
return !user || !session || userNotFullyAuthenticated(user, session);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 06:12:36 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if the user is fully authenticated.
|
|
|
|
|
*
|
|
|
|
|
* @param {User | null} user - The user object.
|
|
|
|
|
* @param {Session | null} session - The session object.
|
|
|
|
|
* @returns {boolean} True if the user is fully authenticated, otherwise false.
|
|
|
|
|
*/
|
2024-06-17 20:06:45 +00:00
|
|
|
export function userFullyAuthenticated(user: User | null, session: Session | null) {
|
|
|
|
|
return !userNotAuthenticated(user, session);
|
2024-06-12 02:12:12 +00:00
|
|
|
}
|