34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import type { RequestHandler } from "./$types"
|
|
|
|
import { error, json, text } from "@sveltejs/kit"
|
|
|
|
import UserMgmt from "$lib/server/usermgmt"
|
|
|
|
import { LoginResponseCause } from "$lib/errors"
|
|
|
|
export const POST: RequestHandler = async ({ request, cookies }) => {
|
|
|
|
const data = await request.formData()
|
|
|
|
const email = data.get("email")
|
|
const password = data.get("password")
|
|
|
|
if (data.keys.length > 2 || !(typeof email === "string" && typeof password === "string")) {
|
|
return error(400, { cause: LoginResponseCause.MalformedRequest, message: "Invalid request" })
|
|
}
|
|
|
|
if (email.length == 0) {
|
|
return error(400, { cause: LoginResponseCause.EmailLength, message: "Email must be provided" })
|
|
}
|
|
if (password.length == 0) {
|
|
return error(400, { cause: LoginResponseCause.PasswordLength, message: "Password must be provided" })
|
|
}
|
|
|
|
const session = await UserMgmt.login(email, password)
|
|
if (!session) {
|
|
return error(401, { message: "Invalid username or password" })
|
|
}
|
|
|
|
return json({ token: session.token, expires: session.expires })
|
|
}
|