2024-03-02 02:00:27 +00:00
|
|
|
import db from "$lib/drizzle";
|
|
|
|
|
import { eq } from "drizzle-orm";
|
|
|
|
|
import { generateId } from "lucia";
|
|
|
|
|
import { TimeSpan, createDate } from "oslo";
|
2024-03-12 18:34:39 +00:00
|
|
|
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));
|
|
|
|
|
const tokenId = generateId(40);
|
|
|
|
|
await db
|
|
|
|
|
.insert(password_reset_tokens)
|
|
|
|
|
.values({
|
|
|
|
|
id: tokenId,
|
|
|
|
|
user_id: userId,
|
|
|
|
|
expires_at: createDate(new TimeSpan(2, "h"))
|
|
|
|
|
});
|
|
|
|
|
return tokenId;
|
|
|
|
|
}
|