many changes idek
parent
f96e9cdd0b
commit
039a4f500c
Binary file not shown.
@ -1,9 +1,8 @@
|
|||||||
import Elysia from "elysia";
|
import Elysia from "elysia";
|
||||||
import { todosController } from "./todos";
|
import { todosController } from "./todos";
|
||||||
import { authController } from "./auth";
|
// import { authController } from "./auth";
|
||||||
|
|
||||||
export const api = new Elysia({
|
export const api = new Elysia({
|
||||||
prefix: "/api",
|
prefix: "/api",
|
||||||
})
|
}).use(todosController);
|
||||||
.use(todosController)
|
// .use(authController);
|
||||||
.use(authController);
|
|
||||||
|
@ -1,129 +1,128 @@
|
|||||||
import { Elysia, t } from "elysia";
|
// import { Elysia, t } from "elysia";
|
||||||
import { ctx } from "../context";
|
// import { ctx } from "../context";
|
||||||
import { set } from "zod";
|
// import { set } from "zod";
|
||||||
import { LuciaError } from "lucia";
|
// import { LuciaError } from "lucia";
|
||||||
|
|
||||||
class DuplicateEmailError extends Error {
|
// class DuplicateEmailError extends Error {
|
||||||
constructor() {
|
// constructor() {
|
||||||
super("Duplicate email");
|
// super("Duplicate email");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
export const authController = new Elysia({
|
// export const authController = new Elysia({
|
||||||
prefix: "/auth",
|
// prefix: "/auth",
|
||||||
})
|
// })
|
||||||
.use(ctx)
|
// .use(ctx)
|
||||||
.post(
|
// .post(
|
||||||
"/signup",
|
// "/signup",
|
||||||
async ({ body: { email, password }, auth, set, cookie }) => {
|
// async ({ body: { email, password }, auth, set }) => {
|
||||||
const user = await auth
|
// const user = await auth
|
||||||
.createUser({
|
// .createUser({
|
||||||
key: {
|
// key: {
|
||||||
providerId: "email", // auth method
|
// providerId: "email", // auth method
|
||||||
providerUserId: email.toLowerCase(), // unique id when using "email" auth method
|
// providerUserId: email.toLowerCase(), // unique id when using "email" auth method
|
||||||
password, // hashed by Lucia
|
// password, // hashed by Lucia
|
||||||
},
|
// },
|
||||||
attributes: {
|
// attributes: {
|
||||||
email,
|
// email,
|
||||||
},
|
// },
|
||||||
})
|
// })
|
||||||
.catch((err) => {
|
// .catch((err) => {
|
||||||
if (err.code === "SQLITE_CONSTRAINT") {
|
// if (err.code === "SQLITE_CONSTRAINT") {
|
||||||
throw new DuplicateEmailError();
|
// throw new DuplicateEmailError();
|
||||||
} else {
|
// } else {
|
||||||
throw err;
|
// throw err;
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
const session = await auth.createSession({
|
// const session = await auth.createSession({
|
||||||
userId: user.userId,
|
// userId: user.userId,
|
||||||
attributes: {},
|
// attributes: {},
|
||||||
});
|
// });
|
||||||
|
|
||||||
const sessionCookie = auth.createSessionCookie(session);
|
// const sessionCookie = auth.createSessionCookie(session);
|
||||||
|
|
||||||
// cookie.session?.set(sessionCookie);
|
// // cookie.session?.set(sessionCookie);
|
||||||
|
|
||||||
set.headers["Set-Cookie"] = sessionCookie.serialize();
|
// set.headers["Set-Cookie"] = sessionCookie.serialize();
|
||||||
set.headers["HX-Location"] = "/profile";
|
// set.headers["HX-Location"] = "/profile";
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
body: t.Object({
|
// body: t.Object({
|
||||||
email: t.String({
|
// email: t.String({
|
||||||
minLength: 5,
|
// minLength: 5,
|
||||||
maxLength: 30,
|
// maxLength: 30,
|
||||||
}),
|
// }),
|
||||||
password: t.String({
|
// password: t.String({
|
||||||
minLength: 6,
|
// minLength: 6,
|
||||||
maxLength: 255,
|
// maxLength: 255,
|
||||||
}),
|
// }),
|
||||||
}),
|
// }),
|
||||||
error({ code, error, set }) {
|
// error({ code, error, set }) {
|
||||||
if (code === "VALIDATION") {
|
// if (code === "VALIDATION") {
|
||||||
console.log("sign up validation error");
|
// console.log("sign up validation error");
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
set.status = 400;
|
// set.status = 400;
|
||||||
return "Invalid email or password";
|
// return "Invalid email or password";
|
||||||
} else if (error instanceof DuplicateEmailError) {
|
// } else if (error instanceof DuplicateEmailError) {
|
||||||
console.log("sign up duplicate email error");
|
// console.log("sign up duplicate email error");
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
set.status = 400;
|
// set.status = 400;
|
||||||
return "Email already exists";
|
// return "Email already exists";
|
||||||
} else {
|
// } else {
|
||||||
console.log("sign up error");
|
// console.log("sign up error");
|
||||||
console.log(error);
|
// console.log(error);
|
||||||
set.status = 500;
|
// set.status = 500;
|
||||||
return "Internal server error";
|
// return "Internal server error";
|
||||||
}
|
// }
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
)
|
// )
|
||||||
.post(
|
// .post(
|
||||||
"/signin",
|
// "/signin",
|
||||||
async ({ body: { email, password }, auth, set, cookie }) => {
|
// async ({ body: { email, password }, auth, set }) => {
|
||||||
const user = await auth.useKey("email", email.toLowerCase(), password);
|
// const user = await auth.useKey("email", email.toLowerCase(), password);
|
||||||
|
|
||||||
const session = await auth.createSession({
|
// const session = await auth.createSession({
|
||||||
userId: user.userId,
|
// userId: user.userId,
|
||||||
attributes: {},
|
// attributes: {},
|
||||||
});
|
// });
|
||||||
const sessionCookie = auth.createSessionCookie(session);
|
// const sessionCookie = auth.createSessionCookie(session);
|
||||||
|
|
||||||
// cookie.sesion?.set(sessionCookie);
|
// set.headers["Set-Cookie"] = sessionCookie.serialize();
|
||||||
set.headers["Set-Cookie"] = sessionCookie.serialize();
|
// set.headers["HX-Location"] = "/profile";
|
||||||
set.headers["HX-Location"] = "/profile";
|
// },
|
||||||
},
|
// {
|
||||||
{
|
// body: t.Object({
|
||||||
body: t.Object({
|
// email: t.String({
|
||||||
email: t.String({
|
// minLength: 5,
|
||||||
minLength: 5,
|
// maxLength: 30,
|
||||||
maxLength: 30,
|
// }),
|
||||||
}),
|
// password: t.String({
|
||||||
password: t.String({
|
// minLength: 6,
|
||||||
minLength: 6,
|
// maxLength: 255,
|
||||||
maxLength: 255,
|
// }),
|
||||||
}),
|
// }),
|
||||||
}),
|
// error({ code, error, set }) {
|
||||||
error({ code, error, set }) {
|
// if (code === "VALIDATION") {
|
||||||
if (code === "VALIDATION") {
|
// console.log("sign up validation error");
|
||||||
console.log("sign up validation error");
|
// console.log(error);
|
||||||
console.log(error);
|
// set.status = 400;
|
||||||
set.status = 400;
|
// return "Invalid email or password";
|
||||||
return "Invalid email or password";
|
// } else if (
|
||||||
} else if (
|
// error instanceof LuciaError &&
|
||||||
error instanceof LuciaError &&
|
// (error.message === "AUTH_INVALID_KEY_ID" ||
|
||||||
(error.message === "AUTH_INVALID_KEY_ID" ||
|
// error.message === "AUTH_INVALID_PASSWORD")
|
||||||
error.message === "AUTH_INVALID_PASSWORD")
|
// ) {
|
||||||
) {
|
// console.log("sign in invalid email or password error");
|
||||||
console.log("sign in invalid email or password error");
|
// console.log(error);
|
||||||
console.log(error);
|
// set.status = 400;
|
||||||
set.status = 400;
|
// return "Invalid email or password";
|
||||||
return "Invalid email or password";
|
// } else {
|
||||||
} else {
|
// console.log("sign up error");
|
||||||
console.log("sign up error");
|
// console.log(error);
|
||||||
console.log(error);
|
// set.status = 500;
|
||||||
set.status = 500;
|
// return "Internal server error";
|
||||||
return "Internal server error";
|
// }
|
||||||
}
|
// },
|
||||||
},
|
// }
|
||||||
}
|
// );
|
||||||
);
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import Elysia from "elysia";
|
import Elysia from "elysia";
|
||||||
import { signup } from "./signup";
|
// import { signup } from "./signup";
|
||||||
import { profile } from "./profile";
|
import { profile } from "./profile";
|
||||||
import { signin } from "./signin";
|
// import { signin } from "./signin";
|
||||||
|
|
||||||
export const authGroup = new Elysia().use(signup).use(signin).use(profile);
|
export const authGroup = new Elysia()
|
||||||
|
// .use(signup).use(signin)
|
||||||
|
.use(profile);
|
||||||
|
@ -1,54 +1,54 @@
|
|||||||
import Elysia from "elysia";
|
// import Elysia from "elysia";
|
||||||
import { BaseHtml } from "../../components/base";
|
// import { BaseHtml } from "../../components/base";
|
||||||
import { ctx } from "../../context";
|
// import { ctx } from "../../context";
|
||||||
|
|
||||||
export const signin = new Elysia().use(ctx).get("/signin", ({ html }) =>
|
// export const signin = new Elysia().use(ctx).get("/signin", ({ html }) =>
|
||||||
html(
|
// html(
|
||||||
<BaseHtml>
|
// <BaseHtml>
|
||||||
<div class="flex w-full h-screen bg-gray-200 justify-center items-center">
|
// <div class="flex w-full h-screen bg-gray-200 justify-center items-center">
|
||||||
<form
|
// <form
|
||||||
hx-post="/api/auth/signin"
|
// hx-post="/api/auth/signin"
|
||||||
hx-swap="afterend"
|
// hx-swap="afterend"
|
||||||
class="bg-white p-8 rounded-lg shadow-md w-96"
|
// class="bg-white p-8 rounded-lg shadow-md w-96"
|
||||||
>
|
// >
|
||||||
<div class="mb-4">
|
// <div class="mb-4">
|
||||||
<label
|
// <label
|
||||||
for="email"
|
// for="email"
|
||||||
class="block text-sm font-medium text-gray-600 mb-2"
|
// class="block text-sm font-medium text-gray-600 mb-2"
|
||||||
>
|
// >
|
||||||
Email
|
// Email
|
||||||
</label>
|
// </label>
|
||||||
<input
|
// <input
|
||||||
type="text"
|
// type="text"
|
||||||
name="email"
|
// name="email"
|
||||||
id="email"
|
// id="email"
|
||||||
placeholder="Enter your email"
|
// placeholder="Enter your email"
|
||||||
class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
// class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
||||||
/>
|
// />
|
||||||
</div>
|
// </div>
|
||||||
<div class="mb-4">
|
// <div class="mb-4">
|
||||||
<label
|
// <label
|
||||||
for="password"
|
// for="password"
|
||||||
class="block text-sm font-medium text-gray-600 mb-2"
|
// class="block text-sm font-medium text-gray-600 mb-2"
|
||||||
>
|
// >
|
||||||
Password
|
// Password
|
||||||
</label>
|
// </label>
|
||||||
<input
|
// <input
|
||||||
type="password"
|
// type="password"
|
||||||
name="password"
|
// name="password"
|
||||||
id="password"
|
// id="password"
|
||||||
placeholder="Enter your password"
|
// placeholder="Enter your password"
|
||||||
class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
// class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
||||||
/>
|
// />
|
||||||
</div>
|
// </div>
|
||||||
<button
|
// <button
|
||||||
type="submit"
|
// type="submit"
|
||||||
class="w-full bg-indigo-600 text-white p-2 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-50"
|
// class="w-full bg-indigo-600 text-white p-2 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-50"
|
||||||
>
|
// >
|
||||||
Sign In
|
// Sign In
|
||||||
</button>
|
// </button>
|
||||||
</form>
|
// </form>
|
||||||
</div>
|
// </div>
|
||||||
</BaseHtml>
|
// </BaseHtml>
|
||||||
)
|
// )
|
||||||
);
|
// );
|
||||||
|
@ -1,54 +1,54 @@
|
|||||||
import Elysia from "elysia";
|
// import Elysia from "elysia";
|
||||||
import { BaseHtml } from "../../components/base";
|
// import { BaseHtml } from "../../components/base";
|
||||||
import { ctx } from "../../context";
|
// import { ctx } from "../../context";
|
||||||
|
|
||||||
export const signup = new Elysia().use(ctx).get("/signup", ({ html }) =>
|
// export const signup = new Elysia().use(ctx).get("/signup", ({ html }) =>
|
||||||
html(
|
// html(
|
||||||
<BaseHtml>
|
// <BaseHtml>
|
||||||
<div class="flex w-full h-screen bg-gray-200 justify-center items-center">
|
// <div class="flex w-full h-screen bg-gray-200 justify-center items-center">
|
||||||
<form
|
// <form
|
||||||
hx-post="/api/auth/signup"
|
// hx-post="/api/auth/signup"
|
||||||
hx-swap="afterend"
|
// hx-swap="afterend"
|
||||||
class="bg-white p-8 rounded-lg shadow-md w-96"
|
// class="bg-white p-8 rounded-lg shadow-md w-96"
|
||||||
>
|
// >
|
||||||
<div class="mb-4">
|
// <div class="mb-4">
|
||||||
<label
|
// <label
|
||||||
for="email"
|
// for="email"
|
||||||
class="block text-sm font-medium text-gray-600 mb-2"
|
// class="block text-sm font-medium text-gray-600 mb-2"
|
||||||
>
|
// >
|
||||||
Email
|
// Email
|
||||||
</label>
|
// </label>
|
||||||
<input
|
// <input
|
||||||
type="text"
|
// type="text"
|
||||||
name="email"
|
// name="email"
|
||||||
id="email"
|
// id="email"
|
||||||
placeholder="Enter your email"
|
// placeholder="Enter your email"
|
||||||
class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
// class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
||||||
/>
|
// />
|
||||||
</div>
|
// </div>
|
||||||
<div class="mb-4">
|
// <div class="mb-4">
|
||||||
<label
|
// <label
|
||||||
for="password"
|
// for="password"
|
||||||
class="block text-sm font-medium text-gray-600 mb-2"
|
// class="block text-sm font-medium text-gray-600 mb-2"
|
||||||
>
|
// >
|
||||||
Password
|
// Password
|
||||||
</label>
|
// </label>
|
||||||
<input
|
// <input
|
||||||
type="password"
|
// type="password"
|
||||||
name="password"
|
// name="password"
|
||||||
id="password"
|
// id="password"
|
||||||
placeholder="Enter your password"
|
// placeholder="Enter your password"
|
||||||
class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
// class="w-full p-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
|
||||||
/>
|
// />
|
||||||
</div>
|
// </div>
|
||||||
<button
|
// <button
|
||||||
type="submit"
|
// type="submit"
|
||||||
class="w-full bg-indigo-600 text-white p-2 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-50"
|
// class="w-full bg-indigo-600 text-white p-2 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-400 focus:ring-opacity-50"
|
||||||
>
|
// >
|
||||||
Sign Up
|
// Sign Up
|
||||||
</button>
|
// </button>
|
||||||
</form>
|
// </form>
|
||||||
</div>
|
// </div>
|
||||||
</BaseHtml>
|
// </BaseHtml>
|
||||||
)
|
// )
|
||||||
);
|
// );
|
||||||
|
Loading…
Reference in New Issue