Files
crates/crates/routes/api/auth/login.ts
T
2025-05-02 01:32:28 +10:00

52 lines
1.6 KiB
TypeScript

import { Handlers } from "$fresh/server.ts"
import { setCookie } from "$std/http/cookie.ts"
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts"
import { z } from "npm:zod"
import { zx } from "npm:zodix"
import { createSession } from "api/mw.auth.ts"
import { InvalidCredentialsError } from "api/errors.ts"
import { CratesState } from 'api/mw.service.ts'
const LoginSchema = z.object({
email: z.string().email("Invalid email format"),
password: z.string().min(1)
})
export const handler: Handlers<{email: string}, CratesState> = {
async POST(req, ctx) {
const { email, password } = await zx.parseForm(req, LoginSchema)
// Create pg instance for database operations
const { pg } = ctx.state
// Find user by email
const user = await pg.userFindEmail(email)
if (!user) throw new InvalidCredentialsError()
// Verify password
const match = await bcrypt.compare(password, user.password)
if (!match) throw new InvalidCredentialsError()
// Create session using Deno KV
const { sessionId, expiresAt } = await createSession(user.id, 24 * 7) // 7 days in hours
// Set cookie
const headers = new Headers({ "Content-Type": "application/json" })
setCookie(headers, {
name: "session",
value: sessionId,
expires: expiresAt,
path: "/",
httpOnly: true,
secure: true,
sameSite: "Lax"
})
return new Response(null, {
status: 302,
headers: { Location: "/home" }
});
},
}