Spaces:
Sleeping
Sleeping
Download apps/api/src/routes/auth.ts from raihan013/eduboard-api: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/raihan013/eduboard-api/resolve/main/apps/api/src/routes/auth.ts
- Command line
-
hf download hf://spaces/raihan013/eduboard-api/apps/api/src/routes/auth.ts
-
curl -L -o auth.ts https://huggingface.co/spaces/raihan013/eduboard-api/resolve/main/apps/api/src/routes/auth.ts
2.62 kB
| import { FastifyInstance } from "fastify"; | |
| import { z } from "zod"; | |
| import bcrypt from "bcryptjs"; | |
| import { prisma } from "@repo/db"; | |
| const LoginSchema = z.object({ | |
| email: z.string().email("Format email tidak valid"), | |
| password: z.string().min(6, "Password minimal 6 karakter"), | |
| }); | |
| const RegisterSchema = z.object({ | |
| email: z.string().email("Format email tidak valid"), | |
| password: z.string().min(6, "Password minimal 6 karakter"), | |
| name: z.string().min(2, "Nama minimal 2 karakter").max(50, "Nama terlalu panjang"), | |
| }); | |
| export default async function authRoutes(fastify: FastifyInstance) { | |
| // LOGIN | |
| fastify.post("/login", async (request, reply) => { | |
| try { | |
| const { email, password } = LoginSchema.parse(request.body); | |
| const user = await prisma.user.findUnique({ where: { email } }); | |
| if (!user) { | |
| return reply.code(401).send({ error: "Email atau password salah" }); | |
| } | |
| const match = await bcrypt.compare(password, user.passwordHash); | |
| if (!match) { | |
| return reply.code(401).send({ error: "Email atau password salah" }); | |
| } | |
| const token = fastify.jwt.sign({ id: user.id, email: user.email }); | |
| return { | |
| token, | |
| user: { | |
| id: user.id, | |
| name: user.name, | |
| email: user.email | |
| } | |
| }; | |
| } catch (err) { | |
| if (err instanceof z.ZodError) { | |
| return reply.code(400).send({ error: err.errors[0].message }); | |
| } | |
| return reply.code(500).send({ error: "Terjadi kesalahan pada server" }); | |
| } | |
| }); | |
| // REGISTER | |
| fastify.post("/register", async (request, reply) => { | |
| try { | |
| const { email, password, name } = RegisterSchema.parse(request.body); | |
| // Check if user exists | |
| const existing = await prisma.user.findUnique({ where: { email } }); | |
| if (existing) { | |
| return reply.code(400).send({ error: "Email sudah terdaftar" }); | |
| } | |
| const passwordHash = await bcrypt.hash(password, 10); | |
| const user = await prisma.user.create({ | |
| data: { | |
| email, | |
| passwordHash, | |
| name | |
| } | |
| }); | |
| const token = fastify.jwt.sign({ id: user.id, email: user.email }); | |
| return { | |
| success: true, | |
| token, | |
| user: { | |
| id: user.id, | |
| name: user.name, | |
| email: user.email | |
| } | |
| }; | |
| } catch (err) { | |
| if (err instanceof z.ZodError) { | |
| return reply.code(400).send({ error: err.errors[0].message }); | |
| } | |
| console.error(err); | |
| return reply.code(500).send({ error: "Gagal mendaftarkan akun" }); | |
| } | |
| }); | |
| } | |