From f6e9f698590113bd17b5403746084967594724d3 Mon Sep 17 00:00:00 2001 From: peintnermax Date: Thu, 21 Mar 2024 16:03:07 +0100 Subject: [PATCH] signin with context --- .../(login)/idp/[provider]/success/page.tsx | 3 +- apps/login/app/(login)/idp/page.tsx | 9 ++- apps/login/app/api/session/route.ts | 1 - apps/login/ui/SignInWithIDP.tsx | 7 +- apps/login/utils/cookies.ts | 81 ++++++++++--------- 5 files changed, 60 insertions(+), 41 deletions(-) diff --git a/apps/login/app/(login)/idp/[provider]/success/page.tsx b/apps/login/app/(login)/idp/[provider]/success/page.tsx index 2101a1f1..511adf16 100644 --- a/apps/login/app/(login)/idp/[provider]/success/page.tsx +++ b/apps/login/app/(login)/idp/[provider]/success/page.tsx @@ -89,7 +89,7 @@ export default async function Page({ searchParams: Record; params: { provider: ProviderSlug }; }) { - const { id, token } = searchParams; + const { id, token, authRequestId } = searchParams; const { provider } = params; if (provider && id && token) { @@ -107,6 +107,7 @@ export default async function Page({ ); diff --git a/apps/login/app/(login)/idp/page.tsx b/apps/login/app/(login)/idp/page.tsx index 3a997321..04661a6b 100644 --- a/apps/login/app/(login)/idp/page.tsx +++ b/apps/login/app/(login)/idp/page.tsx @@ -22,7 +22,13 @@ function getIdentityProviders( }); } -export default async function Page() { +export default async function Page({ + searchParams, +}: { + searchParams: Record; +}) { + const authRequestId = searchParams?.authRequestId; + const legal = await getLegalAndSupportSettings(server); // TODO if org idps should be shown replace emptystring with the orgId. @@ -43,6 +49,7 @@ export default async function Page() { )} diff --git a/apps/login/app/api/session/route.ts b/apps/login/app/api/session/route.ts index b9108801..0337d05b 100644 --- a/apps/login/app/api/session/route.ts +++ b/apps/login/app/api/session/route.ts @@ -73,7 +73,6 @@ export async function PUT(request: NextRequest) { return recentPromise .then((recent) => { - console.log("setsession", webAuthN); return setSessionAndUpdateCookie( recent, password, diff --git a/apps/login/ui/SignInWithIDP.tsx b/apps/login/ui/SignInWithIDP.tsx index 628c4818..843d4f46 100644 --- a/apps/login/ui/SignInWithIDP.tsx +++ b/apps/login/ui/SignInWithIDP.tsx @@ -15,6 +15,7 @@ export interface SignInWithIDPProps { children?: ReactNode; host: string; identityProviders: any[]; + authRequestId?: string; startIDPFlowPath?: (idpId: string) => string; } @@ -24,6 +25,7 @@ const START_IDP_FLOW_PATH = (idpId: string) => export function SignInWithIDP({ host, identityProviders, + authRequestId, startIDPFlowPath = START_IDP_FLOW_PATH, }: SignInWithIDPProps) { const [loading, setLoading] = useState(false); @@ -40,7 +42,10 @@ export function SignInWithIDP({ }, body: JSON.stringify({ idpId, - successUrl: `${host}/idp/${provider}/success`, + successUrl: authRequestId + ? `${host}/idp/${provider}/success?` + + new URLSearchParams({ authRequestId }) + : `${host}/idp/${provider}/success`, failureUrl: `${host}/idp/${provider}/failure`, }), }); diff --git a/apps/login/utils/cookies.ts b/apps/login/utils/cookies.ts index ec355ec4..ec9dc994 100644 --- a/apps/login/utils/cookies.ts +++ b/apps/login/utils/cookies.ts @@ -44,15 +44,15 @@ export async function addSessionToCookie( currentSessions = [...currentSessions, session]; } - if (cleanup) { - const now = new Date(); - const filteredSessions = currentSessions.filter( - (session) => new Date(session.expirationDate) > now - ); - return setSessionHttpOnlyCookie(filteredSessions); - } else { - return setSessionHttpOnlyCookie(currentSessions); - } + // if (cleanup) { + // const now = new Date(); + // const filteredSessions = currentSessions.filter( + // (session) => new Date(session.expirationDate) > now + // ); + // return setSessionHttpOnlyCookie(filteredSessions); + // } else { + return setSessionHttpOnlyCookie(currentSessions); + // } } export async function updateSessionCookie( @@ -71,15 +71,15 @@ export async function updateSessionCookie( if (foundIndex > -1) { sessions[foundIndex] = session; - if (cleanup) { - const now = new Date(); - const filteredSessions = sessions.filter( - (session) => new Date(session.expirationDate) > now - ); - return setSessionHttpOnlyCookie(filteredSessions); - } else { - return setSessionHttpOnlyCookie(sessions); - } + // if (cleanup) { + // const now = new Date(); + // const filteredSessions = sessions.filter( + // (session) => new Date(session.expirationDate) > now + // ); + // return setSessionHttpOnlyCookie(filteredSessions); + // } else { + return setSessionHttpOnlyCookie(sessions); + // } } else { throw "updateSessionCookie: session id now found"; } @@ -97,15 +97,15 @@ export async function removeSessionFromCookie( : [session]; const reducedSessions = sessions.filter((s) => s.id !== session.id); - if (cleanup) { - const now = new Date(); - const filteredSessions = reducedSessions.filter( - (session) => new Date(session.expirationDate) > now - ); - return setSessionHttpOnlyCookie(filteredSessions); - } else { - return setSessionHttpOnlyCookie(reducedSessions); - } + // if (cleanup) { + // const now = new Date(); + // const filteredSessions = reducedSessions.filter( + // (session) => new Date(session.expirationDate) > now + // ); + // return setSessionHttpOnlyCookie(filteredSessions); + // } else { + return setSessionHttpOnlyCookie(reducedSessions); + // } } export async function getMostRecentSessionCookie(): Promise { @@ -180,12 +180,14 @@ export async function getAllSessionCookieIds( if (stringifiedCookie?.value) { const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); - return sessions - .filter((session) => { - const now = new Date(); - cleanup ? new Date(session.expirationDate) > now : true; - }) - .map((session) => session.id); + // if (cleanup) { + // const now = new Date(); + // return sessions + // .filter((session) => new Date(session.expirationDate) > now) + // .map((session) => session.id); + // } else { + return sessions.map((session) => session.id); + // } } else { return []; } @@ -204,10 +206,15 @@ export async function getAllSessions( if (stringifiedCookie?.value) { const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); - return sessions.filter((session) => { - const now = new Date(); - cleanup ? new Date(session.expirationDate) > now : true; - }); + + // if (cleanup) { + // const now = new Date(); + // return sessions.filter( + // (session) => new Date(session.expirationDate) > now + // ); + // } else { + return sessions; + // } } else { return []; }