feat: Implement comprehensive course analytics, RBAC with roles and authentication, and dynamic passing thresholds.

This commit is contained in:
2025-12-23 10:12:53 -03:00
parent f592f78b6c
commit 72ddb43fd7
29 changed files with 1433 additions and 231 deletions
+132 -61
View File
@@ -1,98 +1,169 @@
"use client";
import { useState } from "react";
import { cmsApi } from "@/lib/api";
import { useAuth } from "@/context/AuthContext";
import React, { useState } from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { LogIn, Mail, Lock } from "lucide-react";
import { cmsApi } from "@/lib/api";
import { BookOpen, Lock, Mail, User } from "lucide-react";
export default function LoginPage() {
export default function StudioLoginPage() {
const router = useRouter();
const [isLogin, setIsLogin] = useState(true);
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [fullName, setFullName] = useState("");
const [loading, setLoading] = useState(false);
const { login } = useAuth();
const router = useRouter();
const [error, setError] = useState("");
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError("");
setLoading(true);
try {
const res = await cmsApi.login({ email, password });
login(res.user, res.token);
router.push("/");
if (isLogin) {
const response = await cmsApi.login({ email, password });
// Verify user is instructor or admin
if (response.user.role !== "instructor" && response.user.role !== "admin") {
setError("Access denied. This portal is for instructors and administrators only.");
setLoading(false);
return;
}
localStorage.setItem("studio_token", response.token);
localStorage.setItem("studio_user", JSON.stringify(response.user));
router.push("/");
} else {
const response = await cmsApi.register({
email,
password,
full_name: fullName,
role: "instructor"
});
localStorage.setItem("studio_token", response.token);
localStorage.setItem("studio_user", JSON.stringify(response.user));
router.push("/");
}
} catch (err) {
const message = err instanceof Error ? err.message : "Authentication failed. Please verify your credentials.";
setError(message);
setError(err instanceof Error ? err.message : "Authentication failed");
} finally {
setLoading(false);
}
};
return (
<div className="min-h-[calc(100vh-160px)] flex items-center justify-center p-6">
<div className="w-full max-w-md space-y-8 animate-in fade-in slide-in-from-bottom-4 duration-700">
<div className="text-center space-y-2">
<div className="w-16 h-16 rounded-2xl bg-blue-600/10 border border-blue-500/20 flex items-center justify-center mx-auto text-blue-500 mb-6">
<LogIn size={32} />
<div className="min-h-screen bg-gradient-to-br from-gray-950 via-blue-950 to-gray-950 flex items-center justify-center p-4">
<div className="w-full max-w-md">
{/* Header */}
<div className="text-center mb-8">
<div className="inline-flex items-center justify-center w-16 h-16 bg-blue-600 rounded-2xl mb-4">
<BookOpen className="w-8 h-8 text-white" />
</div>
<h1 className="text-3xl font-black tracking-tighter text-white">Studio Login</h1>
<p className="text-gray-500 font-bold uppercase tracking-widest text-[10px]">Access your educational dashboard</p>
<h1 className="text-3xl font-black text-white mb-2">OpenCCB Studio</h1>
<p className="text-gray-400">Instructor & Administrator Portal</p>
</div>
<div className="glass-card p-8 border-white/5 bg-white/[0.02] rounded-3xl">
<form onSubmit={handleSubmit} className="space-y-6">
{/* Login/Register Form */}
<div className="bg-white/5 backdrop-blur-xl border border-white/10 rounded-3xl p-8">
<div className="flex gap-2 mb-6 bg-white/5 rounded-xl p-1">
<button
onClick={() => setIsLogin(true)}
className={`flex-1 py-2 px-4 rounded-lg font-bold transition-all ${isLogin ? "bg-blue-600 text-white" : "text-gray-400 hover:text-white"
}`}
>
Login
</button>
<button
onClick={() => setIsLogin(false)}
className={`flex-1 py-2 px-4 rounded-lg font-bold transition-all ${!isLogin ? "bg-blue-600 text-white" : "text-gray-400 hover:text-white"
}`}
>
Register
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
{!isLogin && (
<div>
<label className="block text-sm font-bold text-gray-300 mb-2">
Full Name
</label>
<div className="relative">
<User className="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" />
<input
type="text"
value={fullName}
onChange={(e) => setFullName(e.target.value)}
className="w-full bg-white/5 border border-white/10 rounded-xl py-3 pl-11 pr-4 text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="John Doe"
required
/>
</div>
</div>
)}
<div>
<label className="block text-sm font-bold text-gray-300 mb-2">
Email
</label>
<div className="relative">
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" />
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
className="w-full bg-white/5 border border-white/10 rounded-xl py-3 pl-11 pr-4 text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="instructor@example.com"
required
/>
</div>
</div>
<div>
<label className="block text-sm font-bold text-gray-300 mb-2">
Password
</label>
<div className="relative">
<Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" />
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="w-full bg-white/5 border border-white/10 rounded-xl py-3 pl-11 pr-4 text-white placeholder-gray-500 focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="••••••••"
required
/>
</div>
</div>
{error && (
<div className="p-4 rounded-xl bg-red-500/10 border border-red-500/20 text-red-400 text-xs font-bold text-center">
<div className="bg-red-500/10 border border-red-500/20 rounded-xl p-3 text-red-400 text-sm">
{error}
</div>
)}
<div className="space-y-2">
<label className="text-[10px] font-black uppercase tracking-widest text-gray-500 px-1">Instructor Email</label>
<div className="relative">
<Mail className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-500" size={18} />
<input
type="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="instructor@openccb.com"
className="w-full bg-white/5 border border-white/10 rounded-xl py-4 pl-12 pr-4 text-sm text-white focus:outline-none focus:border-blue-500 transition-all placeholder:text-gray-700"
/>
</div>
</div>
<div className="space-y-2">
<label className="text-[10px] font-black uppercase tracking-widest text-gray-500 px-1">Password</label>
<div className="relative">
<Lock className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-500" size={18} />
<input
type="password"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="••••••••"
className="w-full bg-white/5 border border-white/10 rounded-xl py-4 pl-12 pr-4 text-sm text-white focus:outline-none focus:border-blue-500 transition-all placeholder:text-gray-700"
/>
</div>
</div>
<button
disabled={loading}
type="submit"
className="w-full py-4 bg-blue-600 hover:bg-blue-500 text-white rounded-xl font-black text-xs uppercase tracking-[0.2em] shadow-xl shadow-blue-500/20 disabled:opacity-50 transition-all active:scale-[0.98]"
disabled={loading}
className="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 rounded-xl transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
>
{loading ? "Verifying..." : "Enter Studio"}
{loading ? "Processing..." : isLogin ? "Sign In" : "Create Account"}
</button>
</form>
<div className="mt-6 pt-6 border-t border-white/10 text-center">
<p className="text-sm text-gray-400">
Are you a student?{" "}
<a href="http://localhost:3003/auth/login" className="text-blue-400 hover:text-blue-300 font-bold">
Go to Student Portal
</a>
</p>
</div>
</div>
<p className="text-center text-[10px] font-bold uppercase tracking-widest text-gray-600">
New to Studio? <Link href="/auth/register" className="text-blue-500 hover:text-blue-400">Create an account</Link>
<p className="text-center text-xs text-gray-500 mt-6">
OpenCCB Studio - Instructor & Administrator Portal
</p>
</div>
</div>