mirror of
https://github.com/kyantech/Palmr.git
synced 2025-10-23 06:11:58 +00:00
feat(auth): add protected routes and enhance auth context
Implement protected routes for admin and dashboard pages to restrict access based on authentication and admin status. Enhance the auth context to handle user data validation and improve error handling during authentication checks.
This commit is contained in:
16
apps/app/src/app/admin/page.tsx
Normal file
16
apps/app/src/app/admin/page.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { ProtectedRoute } from "@/components/auth/protected-route";
|
||||
|
||||
function AdminPage() {
|
||||
return (
|
||||
<ProtectedRoute requireAdmin>
|
||||
<div>
|
||||
<h1>Admin Dashboard</h1>
|
||||
{/* Your admin content */}
|
||||
</div>
|
||||
</ProtectedRoute>
|
||||
);
|
||||
}
|
||||
|
||||
export default AdminPage;
|
16
apps/app/src/app/dashboard/page.tsx
Normal file
16
apps/app/src/app/dashboard/page.tsx
Normal file
@@ -0,0 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { ProtectedRoute } from "@/components/auth/protected-route";
|
||||
|
||||
function DashboardPage() {
|
||||
return (
|
||||
<ProtectedRoute>
|
||||
<div>
|
||||
<h1>Dashboard</h1>
|
||||
{/* Your dashboard content */}
|
||||
</div>
|
||||
</ProtectedRoute>
|
||||
);
|
||||
}
|
||||
|
||||
export default DashboardPage;
|
34
apps/app/src/components/auth/protected-route.tsx
Normal file
34
apps/app/src/components/auth/protected-route.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
"use client";
|
||||
|
||||
import { useAuth } from "@/contexts/auth-context";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, type ReactNode } from "react";
|
||||
import { LoadingScreen } from "../layout/loading-screen";
|
||||
|
||||
type ProtectedRouteProps = {
|
||||
children: ReactNode;
|
||||
requireAdmin?: boolean;
|
||||
};
|
||||
|
||||
export function ProtectedRoute({ children, requireAdmin = false }: ProtectedRouteProps) {
|
||||
const { isAuthenticated, isAdmin } = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthenticated === false) {
|
||||
router.replace("/login");
|
||||
} else if (requireAdmin && isAdmin === false) {
|
||||
router.replace("/dashboard");
|
||||
}
|
||||
}, [isAuthenticated, isAdmin, requireAdmin, router]);
|
||||
|
||||
if (isAuthenticated === null || (requireAdmin && isAdmin === null)) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
|
||||
if (!isAuthenticated || (requireAdmin && !isAdmin)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
@@ -41,6 +41,10 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
const response = await getCurrentUser();
|
||||
if (!response?.data?.user) {
|
||||
throw new Error("No user data");
|
||||
}
|
||||
|
||||
const { isAdmin, ...userData } = response.data.user;
|
||||
|
||||
setUser(userData);
|
||||
@@ -48,7 +52,9 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
setIsAuthenticated(true);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
logout();
|
||||
setUser(null);
|
||||
setIsAdmin(false);
|
||||
setIsAuthenticated(false);
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user