Neah version calendar fix 3 debuger sec chance danger debug 2
This commit is contained in:
parent
9eef481490
commit
b5855e5a84
@ -2,28 +2,32 @@
|
||||
|
||||
import { useSession } from "next-auth/react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export function AuthCheck({ children }: { children: React.ReactNode }) {
|
||||
const { data: session, status } = useSession();
|
||||
const pathname = usePathname();
|
||||
const router = useRouter();
|
||||
const [isRedirecting, setIsRedirecting] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (status === "unauthenticated" && pathname !== "/signin" && !isRedirecting) {
|
||||
setIsRedirecting(true);
|
||||
router.push("/signin");
|
||||
// Only redirect if we're certain the user is not authenticated
|
||||
// and we're not already on the signin page
|
||||
if (status === "unauthenticated" && pathname !== "/signin") {
|
||||
router.replace("/signin");
|
||||
}
|
||||
}, [status, router, pathname, isRedirecting]);
|
||||
}, [status, pathname, router]);
|
||||
|
||||
// During loading, show the children to prevent flashing
|
||||
// This works because server-side session check will handle protection
|
||||
if (status === "loading") {
|
||||
return <div>Chargement...</div>;
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
if (status === "unauthenticated" && pathname !== "/signin") {
|
||||
return null;
|
||||
// If we're on the signin page, or if we're authenticated, show the children
|
||||
if (pathname === "/signin" || status === "authenticated") {
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
// Otherwise, render nothing while redirecting
|
||||
return null;
|
||||
}
|
||||
@ -8,7 +8,7 @@ export function SignInForm() {
|
||||
<h1 className="text-4xl font-bold text-white mb-4">Bienvenue sur Enkun</h1>
|
||||
<p className="text-white/80 mb-8">Connectez-vous pour accéder à votre espace</p>
|
||||
<button
|
||||
onClick={() => signIn("keycloak", { callbackUrl: window.location.href })}
|
||||
onClick={() => signIn("keycloak", { callbackUrl: "https://lab.slm-lab.net/" })}
|
||||
className="px-8 py-3 bg-[#0F172A] text-white rounded hover:bg-[#1E293B] transition-colors"
|
||||
>
|
||||
Se connecter
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { SessionProvider } from "next-auth/react";
|
||||
import { authOptions } from "@/lib/auth";
|
||||
|
||||
interface ProvidersProps {
|
||||
children: React.ReactNode;
|
||||
@ -9,7 +8,7 @@ interface ProvidersProps {
|
||||
|
||||
export function Providers({ children }: ProvidersProps) {
|
||||
return (
|
||||
<SessionProvider session={null} refetchInterval={5 * 60} refetchOnWindowFocus={true}>
|
||||
<SessionProvider refetchInterval={5 * 60}>
|
||||
{children}
|
||||
</SessionProvider>
|
||||
);
|
||||
|
||||
26
lib/auth.ts
26
lib/auth.ts
@ -7,6 +7,9 @@ declare module 'next-auth' {
|
||||
email: string;
|
||||
name?: string | null;
|
||||
role: string[];
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
username: string;
|
||||
}
|
||||
interface Session {
|
||||
user: {
|
||||
@ -14,13 +17,21 @@ declare module 'next-auth' {
|
||||
email: string;
|
||||
name?: string | null;
|
||||
role: string[];
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
username: string;
|
||||
};
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
}
|
||||
interface Profile {
|
||||
sub: string;
|
||||
email: string;
|
||||
sub?: string;
|
||||
email?: string;
|
||||
name?: string;
|
||||
roles?: string[];
|
||||
given_name?: string;
|
||||
family_name?: string;
|
||||
preferred_username?: string;
|
||||
}
|
||||
}
|
||||
|
||||
@ -30,6 +41,9 @@ declare module 'next-auth/jwt' {
|
||||
email: string;
|
||||
name?: string;
|
||||
role: string[];
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
username: string;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
accessTokenExpires: number;
|
||||
@ -81,6 +95,9 @@ export const authOptions: NextAuthOptions = {
|
||||
token.email = profile.email || '';
|
||||
token.name = profile.name;
|
||||
token.role = profile.roles || ['user'];
|
||||
token.first_name = profile.given_name || '';
|
||||
token.last_name = profile.family_name || '';
|
||||
token.username = profile.preferred_username || '';
|
||||
token.accessToken = account.access_token;
|
||||
token.refreshToken = account.refresh_token;
|
||||
token.accessTokenExpires = account.expires_at * 1000;
|
||||
@ -150,6 +167,11 @@ export const authOptions: NextAuthOptions = {
|
||||
session.user.email = token.email;
|
||||
session.user.name = token.name;
|
||||
session.user.role = token.role;
|
||||
session.user.first_name = token.first_name;
|
||||
session.user.last_name = token.last_name;
|
||||
session.user.username = token.username;
|
||||
session.accessToken = token.accessToken;
|
||||
session.refreshToken = token.refreshToken;
|
||||
|
||||
return session;
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user