2024-04-25 18:26:05 +00:00
|
|
|
import { generateIdFromEntropySize } from 'lucia';
|
|
|
|
|
import { TimeSpan, createDate } from 'oslo';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
|
import db from '$lib/drizzle';
|
|
|
|
|
import { password_reset_tokens } from '../../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
|
|
|
}
|