Fix UI Admin Menu Pendidikam, Add Menu User & Role
This commit is contained in:
81
src/app/api/[[...slugs]]/_lib/auth/login.ts
Normal file
81
src/app/api/[[...slugs]]/_lib/auth/login.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Context } from "elysia";
|
||||
import prisma from "@/lib/prisma";
|
||||
import bcrypt from "bcryptjs";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
// ENV atau secret key untuk token
|
||||
const JWT_SECRET = process.env.JWT_SECRET || "super-secret-key"; // ganti di env production
|
||||
|
||||
type LoginForm = {
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export default async function userLogin(context: Context) {
|
||||
const body = (await context.body) as LoginForm;
|
||||
|
||||
try {
|
||||
// 1. Cari user berdasarkan email
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { email: body.email },
|
||||
include: { role: true }, // include role untuk otorisasi
|
||||
});
|
||||
|
||||
// 2. Jika tidak ada user
|
||||
if (!user) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Email tidak ditemukan",
|
||||
};
|
||||
}
|
||||
|
||||
// 3. Cek apakah user aktif
|
||||
if (!user.isActive) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Akun tidak aktif",
|
||||
};
|
||||
}
|
||||
|
||||
// 4. Verifikasi password
|
||||
const isMatch = await bcrypt.compare(body.password, user.password);
|
||||
if (!isMatch) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Password salah",
|
||||
};
|
||||
}
|
||||
|
||||
// 5. Buat JWT token
|
||||
const token = jwt.sign(
|
||||
{
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
role: user.role.name,
|
||||
},
|
||||
JWT_SECRET,
|
||||
{ expiresIn: "7d" } // expire 7 hari
|
||||
);
|
||||
|
||||
// 6. Kirim response
|
||||
return {
|
||||
success: true,
|
||||
message: "Login berhasil",
|
||||
data: {
|
||||
user: {
|
||||
id: user.id,
|
||||
nama: user.nama,
|
||||
email: user.email,
|
||||
role: user.role.name,
|
||||
},
|
||||
token,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat login",
|
||||
};
|
||||
}
|
||||
}
|
||||
0
src/app/api/[[...slugs]]/_lib/auth/logout.ts
Normal file
0
src/app/api/[[...slugs]]/_lib/auth/logout.ts
Normal file
88
src/app/api/[[...slugs]]/_lib/auth/register.ts
Normal file
88
src/app/api/[[...slugs]]/_lib/auth/register.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { Context } from "elysia";
|
||||
|
||||
interface RegisterBody {
|
||||
nama: string;
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export default async function userRegister(context: Context) {
|
||||
try {
|
||||
const body = (await context.body) as RegisterBody;
|
||||
|
||||
// Validasi input
|
||||
if (!body.nama || !body.email || !body.password) {
|
||||
context.set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
message: "Semua field harus diisi",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
// Cek email sudah terdaftar
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
where: { email: body.email },
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
context.set.status = 400;
|
||||
return {
|
||||
success: false,
|
||||
message: "Email sudah terdaftar",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
// Dapatkan role warga
|
||||
const role = await prisma.role.findFirst({
|
||||
where: { name: "warga" }
|
||||
});
|
||||
|
||||
if (!role) {
|
||||
context.set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Role warga tidak ditemukan",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(body.password, 10);
|
||||
|
||||
// Buat user baru
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
nama: body.nama,
|
||||
email: body.email,
|
||||
password: hashedPassword,
|
||||
roleId: role.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
nama: true,
|
||||
email: true,
|
||||
roleId: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Berhasil mendaftar",
|
||||
data: user,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Registration error:", error);
|
||||
context.set.status = 500;
|
||||
return {
|
||||
success: false,
|
||||
message: "Terjadi kesalahan saat mendaftar",
|
||||
data: null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user