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 { useSession } from "next-auth/react";
|
||||||
import { usePathname, useRouter } from "next/navigation";
|
import { usePathname, useRouter } from "next/navigation";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export function AuthCheck({ children }: { children: React.ReactNode }) {
|
export function AuthCheck({ children }: { children: React.ReactNode }) {
|
||||||
const { data: session, status } = useSession();
|
const { data: session, status } = useSession();
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [isRedirecting, setIsRedirecting] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (status === "unauthenticated" && pathname !== "/signin" && !isRedirecting) {
|
// Only redirect if we're certain the user is not authenticated
|
||||||
setIsRedirecting(true);
|
// and we're not already on the signin page
|
||||||
router.push("/signin");
|
|
||||||
}
|
|
||||||
}, [status, router, pathname, isRedirecting]);
|
|
||||||
|
|
||||||
if (status === "loading") {
|
|
||||||
return <div>Chargement...</div>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status === "unauthenticated" && pathname !== "/signin") {
|
if (status === "unauthenticated" && pathname !== "/signin") {
|
||||||
return null;
|
router.replace("/signin");
|
||||||
}
|
}
|
||||||
|
}, [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 <>{children}</>;
|
return <>{children}</>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If we're on the signin page, or if we're authenticated, show the children
|
||||||
|
if (pathname === "/signin" || status === "authenticated") {
|
||||||
|
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>
|
<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>
|
<p className="text-white/80 mb-8">Connectez-vous pour accéder à votre espace</p>
|
||||||
<button
|
<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"
|
className="px-8 py-3 bg-[#0F172A] text-white rounded hover:bg-[#1E293B] transition-colors"
|
||||||
>
|
>
|
||||||
Se connecter
|
Se connecter
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { SessionProvider } from "next-auth/react";
|
import { SessionProvider } from "next-auth/react";
|
||||||
import { authOptions } from "@/lib/auth";
|
|
||||||
|
|
||||||
interface ProvidersProps {
|
interface ProvidersProps {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
@ -9,7 +8,7 @@ interface ProvidersProps {
|
|||||||
|
|
||||||
export function Providers({ children }: ProvidersProps) {
|
export function Providers({ children }: ProvidersProps) {
|
||||||
return (
|
return (
|
||||||
<SessionProvider session={null} refetchInterval={5 * 60} refetchOnWindowFocus={true}>
|
<SessionProvider refetchInterval={5 * 60}>
|
||||||
{children}
|
{children}
|
||||||
</SessionProvider>
|
</SessionProvider>
|
||||||
);
|
);
|
||||||
|
|||||||
26
lib/auth.ts
26
lib/auth.ts
@ -7,6 +7,9 @@ declare module 'next-auth' {
|
|||||||
email: string;
|
email: string;
|
||||||
name?: string | null;
|
name?: string | null;
|
||||||
role: string[];
|
role: string[];
|
||||||
|
first_name: string;
|
||||||
|
last_name: string;
|
||||||
|
username: string;
|
||||||
}
|
}
|
||||||
interface Session {
|
interface Session {
|
||||||
user: {
|
user: {
|
||||||
@ -14,13 +17,21 @@ declare module 'next-auth' {
|
|||||||
email: string;
|
email: string;
|
||||||
name?: string | null;
|
name?: string | null;
|
||||||
role: string[];
|
role: string[];
|
||||||
|
first_name: string;
|
||||||
|
last_name: string;
|
||||||
|
username: string;
|
||||||
};
|
};
|
||||||
|
accessToken: string;
|
||||||
|
refreshToken: string;
|
||||||
}
|
}
|
||||||
interface Profile {
|
interface Profile {
|
||||||
sub: string;
|
sub?: string;
|
||||||
email: string;
|
email?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
roles?: string[];
|
roles?: string[];
|
||||||
|
given_name?: string;
|
||||||
|
family_name?: string;
|
||||||
|
preferred_username?: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,6 +41,9 @@ declare module 'next-auth/jwt' {
|
|||||||
email: string;
|
email: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
role: string[];
|
role: string[];
|
||||||
|
first_name: string;
|
||||||
|
last_name: string;
|
||||||
|
username: string;
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
refreshToken: string;
|
refreshToken: string;
|
||||||
accessTokenExpires: number;
|
accessTokenExpires: number;
|
||||||
@ -81,6 +95,9 @@ export const authOptions: NextAuthOptions = {
|
|||||||
token.email = profile.email || '';
|
token.email = profile.email || '';
|
||||||
token.name = profile.name;
|
token.name = profile.name;
|
||||||
token.role = profile.roles || ['user'];
|
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.accessToken = account.access_token;
|
||||||
token.refreshToken = account.refresh_token;
|
token.refreshToken = account.refresh_token;
|
||||||
token.accessTokenExpires = account.expires_at * 1000;
|
token.accessTokenExpires = account.expires_at * 1000;
|
||||||
@ -150,6 +167,11 @@ export const authOptions: NextAuthOptions = {
|
|||||||
session.user.email = token.email;
|
session.user.email = token.email;
|
||||||
session.user.name = token.name;
|
session.user.name = token.name;
|
||||||
session.user.role = token.role;
|
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;
|
return session;
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user