-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogin.ts
More file actions
43 lines (37 loc) · 1.11 KB
/
login.ts
File metadata and controls
43 lines (37 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { auth } from "../../auth";
import { Argon2id } from "oslo/password";
import type { APIContext } from "astro";
import { db } from "../../db";
import { users } from "../../db/schema";
import { eq } from "drizzle-orm";
export async function POST(context: APIContext): Promise<Response> {
const formData = await context.request.formData();
const username = formData.get("username") as string;
const password = formData.get("password") as string;
const existingUser = (
await db
.select()
.from(users)
.where(eq(users.username, username))
.limit(1)
.execute()
)[0];
const validPassword = await new Argon2id().verify(
existingUser.hashed_password!,
password
);
console.log(validPassword);
if (!validPassword) {
return new Response("Incorrect username or password", {
status: 400,
});
}
const session = await auth.createSession(existingUser.id, {});
const sessionCookie = auth.createSessionCookie(session.id);
context.cookies.set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
return context.redirect("/");
}