Skip to content

Commit

Permalink
signin with context
Browse files Browse the repository at this point in the history
  • Loading branch information
peintnermax committed Mar 21, 2024
1 parent ca8bcdb commit f6e9f69
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 41 deletions.
3 changes: 2 additions & 1 deletion apps/login/app/(login)/idp/[provider]/success/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default async function Page({
searchParams: Record<string | number | symbol, string | undefined>;
params: { provider: ProviderSlug };
}) {
const { id, token } = searchParams;
const { id, token, authRequestId } = searchParams;
const { provider } = params;

if (provider && id && token) {
Expand All @@ -107,6 +107,7 @@ export default async function Page({
<IdpSignin
userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/>
</div>
);
Expand Down
9 changes: 8 additions & 1 deletion apps/login/app/(login)/idp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ function getIdentityProviders(
});
}

export default async function Page() {
export default async function Page({
searchParams,
}: {
searchParams: Record<string | number | symbol, string | undefined>;
}) {
const authRequestId = searchParams?.authRequestId;

const legal = await getLegalAndSupportSettings(server);

// TODO if org idps should be shown replace emptystring with the orgId.
Expand All @@ -43,6 +49,7 @@ export default async function Page() {
<SignInWithIDP
host={host}
identityProviders={identityProviders}
authRequestId={authRequestId}
></SignInWithIDP>
)}
</div>
Expand Down
1 change: 0 additions & 1 deletion apps/login/app/api/session/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export async function PUT(request: NextRequest) {

return recentPromise
.then((recent) => {
console.log("setsession", webAuthN);
return setSessionAndUpdateCookie(
recent,
password,
Expand Down
7 changes: 6 additions & 1 deletion apps/login/ui/SignInWithIDP.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface SignInWithIDPProps {
children?: ReactNode;
host: string;
identityProviders: any[];
authRequestId?: string;
startIDPFlowPath?: (idpId: string) => string;
}

Expand All @@ -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<boolean>(false);
Expand All @@ -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`,
}),
});
Expand Down
81 changes: 44 additions & 37 deletions apps/login/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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";
}
Expand All @@ -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<any> {
Expand Down Expand Up @@ -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 [];
}
Expand All @@ -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 [];
}
Expand Down

0 comments on commit f6e9f69

Please sign in to comment.