Skip to content

Commit

Permalink
fix: redirection login
Browse files Browse the repository at this point in the history
  • Loading branch information
jonat75 committed Jan 20, 2025
1 parent bf0c606 commit 412914f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions packages/app/src/app/(default)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { authConfig } from "@api/core-domain/infra/auth/config";
import { getServerSession } from "next-auth";
import { type PropsWithChildren } from "react";

import { Footer } from "../Footer";
import { Header } from "../Header";
// TODO watch https://github.com/vercel/next.js/discussions/49607
// for resolution of "[css url] was preloaded using link preload but not used within a few seconds from the window's load event." warning in dev mode
import styles from "./default.module.css";
import { LoginRedirect } from "./login/LoginRedirect";
import { Navigation } from "./Navigation";

const DefaultLayout = async ({ children }: PropsWithChildren) => {
const session = await getServerSession(authConfig);
return (
<div className={styles.app}>
<LoginRedirect session={session} />
<Header auth navigation={<Navigation />} />
<main role="main" id="content" className={styles.content}>
{children}
Expand Down
20 changes: 14 additions & 6 deletions packages/app/src/app/(default)/login/LoginRedirect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { useRouter } from "next/navigation";
import { type Session } from "next-auth";
import { useEffect } from "react";

Expand All @@ -15,25 +16,32 @@ function getUriFromUrl(urlString: string): string {
}

interface LoginRedirectProps {
callbackUrl: string;
callbackUrl?: string;
session: Session | null;
}

export function LoginRedirect({ session, callbackUrl }: LoginRedirectProps) {
const router = useRouter();

useEffect(() => {
if (callbackUrl) {
localStorage.setItem("egapro_callback_url", callbackUrl);
const callbackUrlObj = new URL(callbackUrl);
const currentOrigin = window.location.origin;
if (callbackUrlObj.origin !== currentOrigin) {
console.log("Invalid callback URL:", callbackUrl);
localStorage.setItem("egapro_callback_url", callbackUrl);
}
}
}, [callbackUrl]);

useEffect(() => {
if (session?.user) {
const savedCallbackUrl = localStorage.getItem("egapro_callback_url");
const savedCallbackUrl = localStorage.getItem("egapro_callback_url");
if (session?.user && savedCallbackUrl) {
const redirectUrl = savedCallbackUrl ? getUriFromUrl(savedCallbackUrl) : "/";
localStorage.removeItem("egapro_callback_url");
window.location.href = redirectUrl;
router.push(redirectUrl);
}
}, [session?.user]);
}, [session?.user, router]);

return null;
}
3 changes: 2 additions & 1 deletion packages/app/src/app/(default)/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getServerSession } from "next-auth";

import { EmailLogin } from "./EmailLogin";
import { GithubLogin } from "./GithubLogin";
import { LoginRedirect } from "./LoginRedirect";
import { ProConnectLogin } from "./ProConnectLogin";

const title = "Connexion";
Expand Down Expand Up @@ -42,7 +43,7 @@ const LoginPage = async ({ searchParams }: NextServerPageProps<never, "callbackU

return (
<CenteredContainer py="6w">
{/*<LoginRedirect session={session} callbackUrl={callbackUrl} />*/}
<LoginRedirect session={session} callbackUrl={callbackUrl} />
<h1>{title}</h1>
{session?.user ? (
<Alert severity="success" title={session?.user.email} description="Vous êtes déjà connecté·e." />
Expand Down

0 comments on commit 412914f

Please sign in to comment.