diff --git a/src/controllers/auth.tsx b/src/controllers/auth.tsx index 36b37cb..92f9010 100644 --- a/src/controllers/auth.tsx +++ b/src/controllers/auth.tsx @@ -1,6 +1,7 @@ import { Elysia, t } from "elysia"; import { ctx } from "../context"; import { set } from "zod"; +import { LuciaError } from "lucia"; class DuplicateEmailError extends Error { constructor() { @@ -14,7 +15,7 @@ export const authController = new Elysia({ .use(ctx) .post( "/signup", - async ({ body: { email, password }, auth, set }) => { + async ({ body: { email, password }, auth, set, cookie }) => { const user = await auth .createUser({ key: { @@ -37,10 +38,12 @@ export const authController = new Elysia({ userId: user.userId, attributes: {}, }); + const sessionCookie = auth.createSessionCookie(session); - set.headers["Set-Cookie"] = sessionCookie.serialize(); - set.redirect = "/profile"; + cookie.sesion?.set(sessionCookie); + + set.headers["HX-Location"] = "/profile"; }, { body: t.Object({ @@ -72,4 +75,53 @@ export const authController = new Elysia({ } }, } + ) + .post( + "/signin", + async ({ body: { email, password }, auth, set, cookie }) => { + const user = await auth.useKey("email", email.toLowerCase(), password); + + const session = await auth.createSession({ + userId: user.userId, + attributes: {}, + }); + const sessionCookie = auth.createSessionCookie(session); + + cookie.sesion?.set(sessionCookie); + set.headers["HX-Location"] = "/profile"; + }, + { + body: t.Object({ + email: t.String({ + minLength: 5, + maxLength: 30, + }), + password: t.String({ + minLength: 6, + maxLength: 255, + }), + }), + error({ code, error, set }) { + if (code === "VALIDATION") { + console.log("sign up validation error"); + console.log(error); + set.status = 400; + return "Invalid email or password"; + } else if ( + error instanceof LuciaError && + (error.message === "AUTH_INVALID_KEY_ID" || + error.message === "AUTH_INVALID_PASSWORD") + ) { + console.log("sign in invalid email or password error"); + console.log(error); + set.status = 400; + return "Invalid email or password"; + } else { + console.log("sign up error"); + console.log(error); + set.status = 500; + return "Internal server error"; + } + }, + } ); diff --git a/src/pages/(auth)/*.ts b/src/pages/(auth)/*.ts index 7135896..3657876 100644 --- a/src/pages/(auth)/*.ts +++ b/src/pages/(auth)/*.ts @@ -1,5 +1,6 @@ import Elysia from "elysia"; import { signup } from "./signup"; import { profile } from "./profile"; +import { signin } from "./signin"; -export const authGroup = new Elysia().use(signup).use(profile); +export const authGroup = new Elysia().use(signup).use(signin).use(profile); diff --git a/src/pages/(auth)/signin.tsx b/src/pages/(auth)/signin.tsx new file mode 100644 index 0000000..3eabd87 --- /dev/null +++ b/src/pages/(auth)/signin.tsx @@ -0,0 +1,54 @@ +import Elysia from "elysia"; +import { BaseHtml } from "../../components/base"; +import { ctx } from "../../context"; + +export const signin = new Elysia().use(ctx).get("/signin", ({ html }) => + html( + +
+
+
+ + +
+
+ + +
+ +
+
+
+ ) +);