.
This commit is contained in:
parent
5d99da0b7c
commit
1fb6a2004d
@ -3,51 +3,48 @@
|
||||
import { useState } from "react";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Lock, User, Shield } from "lucide-react"; // Tambahan ikon Shield untuk Admin
|
||||
import { Lock, User, Shield } from "lucide-react";
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
|
||||
// State untuk menyimpan input user dan pilihan role
|
||||
const [loginRole, setLoginRole] = useState("user"); // "user" atau "admin"
|
||||
const [loginRole, setLoginRole] = useState("user");
|
||||
const [nrp, setNrp] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
// Fungsi saat tombol Login ditekan
|
||||
const handleLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
setError("");
|
||||
|
||||
try {
|
||||
// Tembak API Backend Golang menggunakan IP Laptop
|
||||
const response = await axios.post("http://172.17.172.17:8080/api/auth/login", {
|
||||
nrp_nip: nrp,
|
||||
password: password,
|
||||
// role_attempt: loginRole // Buka komentar ini jika backend Golang minta parameter role
|
||||
});
|
||||
|
||||
const { token, user } = response.data;
|
||||
|
||||
// 1. Simpan di localStorage (untuk dipakai di axios frontend)
|
||||
// Simpan di localStorage untuk kebutuhan frontend
|
||||
localStorage.setItem("token", token);
|
||||
localStorage.setItem("user", JSON.stringify(user));
|
||||
|
||||
// 2. Simpan di Cookie agar bisa dibaca oleh middleware.ts Next.js
|
||||
// Simpan role ke cookie juga agar Middleware Next.js tahu siapa yang sedang login
|
||||
document.cookie = `token=${token}; path=/; max-age=86400; SameSite=Lax`;
|
||||
document.cookie = `userRole=${user.role}; path=/; max-age=86400; SameSite=Lax`;
|
||||
|
||||
// Redirect berdasarkan role dari backend DAN pilihan di UI
|
||||
// Logika Pengalihan Halaman
|
||||
if (user.role === "admin" && loginRole === "admin") {
|
||||
router.push("/admin");
|
||||
} else if (user.role !== "admin" && loginRole === "user") {
|
||||
router.push("/dashboard"); // Halaman pemesanan kelas biasa
|
||||
router.push("/admin/monitoring"); // Langsung arahkan ke halaman utama admin
|
||||
} else if (user.role === "student" && loginRole === "user") {
|
||||
router.push("/dashboard");
|
||||
} else {
|
||||
// Mencegah user biasa login lewat tab admin, atau sebaliknya
|
||||
setError("Role tidak sesuai dengan tipe akun Anda.");
|
||||
localStorage.clear(); // Bersihkan token yang terlanjur masuk
|
||||
document.cookie = "token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"; // Hapus cookie
|
||||
localStorage.clear();
|
||||
document.cookie = "token=; path=/; max-age=0;";
|
||||
document.cookie = "userRole=; path=/; max-age=0;";
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
@ -60,13 +57,11 @@ export default function LoginPage() {
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-gray-100">
|
||||
<div className="w-full max-w-md bg-white p-8 rounded-2xl shadow-lg">
|
||||
{/* Header / Logo */}
|
||||
<div className="text-center mb-6">
|
||||
<h1 className="text-3xl font-bold text-blue-600 mb-2">S-CLASS</h1>
|
||||
<p className="text-gray-500">Smart Classroom Booking System</p>
|
||||
</div>
|
||||
|
||||
{/* --- BAGIAN BARU: Toggle Role --- */}
|
||||
<div className="flex justify-center mb-6 bg-gray-100 p-1 rounded-lg">
|
||||
<button
|
||||
type="button"
|
||||
@ -87,29 +82,21 @@ export default function LoginPage() {
|
||||
Administrator
|
||||
</button>
|
||||
</div>
|
||||
{/* ------------------------------- */}
|
||||
|
||||
{/* Form Login */}
|
||||
<form onSubmit={handleLogin} className="space-y-6">
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-100 text-red-600 p-3 rounded-lg text-sm text-center">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Input NRP / Username dinamis tergantung role */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
{loginRole === "admin" ? "Username Admin" : "NRP / NIP"}
|
||||
</label>
|
||||
<div className="relative">
|
||||
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
||||
{loginRole === "admin" ? (
|
||||
<Shield className="h-5 w-5 text-gray-400" />
|
||||
) : (
|
||||
<User className="h-5 w-5 text-gray-400" />
|
||||
)}
|
||||
{loginRole === "admin" ? <Shield className="h-5 w-5 text-gray-400" /> : <User className="h-5 w-5 text-gray-400" />}
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
@ -122,7 +109,6 @@ export default function LoginPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Input Password */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">Password</label>
|
||||
<div className="relative">
|
||||
@ -140,7 +126,6 @@ export default function LoginPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tombol Login */}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
@ -151,7 +136,6 @@ export default function LoginPage() {
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="mt-6 text-center text-xs text-gray-400">
|
||||
© 2026 Skripsi S-CLASS Project
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user