signin with context

This commit is contained in:
peintnermax
2024-03-21 16:03:07 +01:00
parent ca8bcdb676
commit f6e9f69859
5 changed files with 60 additions and 41 deletions

View File

@@ -89,7 +89,7 @@ export default async function Page({
searchParams: Record<string | number | symbol, string | undefined>; searchParams: Record<string | number | symbol, string | undefined>;
params: { provider: ProviderSlug }; params: { provider: ProviderSlug };
}) { }) {
const { id, token } = searchParams; const { id, token, authRequestId } = searchParams;
const { provider } = params; const { provider } = params;
if (provider && id && token) { if (provider && id && token) {
@@ -107,6 +107,7 @@ export default async function Page({
<IdpSignin <IdpSignin
userId={userId} userId={userId}
idpIntent={{ idpIntentId: id, idpIntentToken: token }} idpIntent={{ idpIntentId: id, idpIntentToken: token }}
authRequestId={authRequestId}
/> />
</div> </div>
); );

View File

@@ -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); const legal = await getLegalAndSupportSettings(server);
// TODO if org idps should be shown replace emptystring with the orgId. // TODO if org idps should be shown replace emptystring with the orgId.
@@ -43,6 +49,7 @@ export default async function Page() {
<SignInWithIDP <SignInWithIDP
host={host} host={host}
identityProviders={identityProviders} identityProviders={identityProviders}
authRequestId={authRequestId}
></SignInWithIDP> ></SignInWithIDP>
)} )}
</div> </div>

View File

@@ -73,7 +73,6 @@ export async function PUT(request: NextRequest) {
return recentPromise return recentPromise
.then((recent) => { .then((recent) => {
console.log("setsession", webAuthN);
return setSessionAndUpdateCookie( return setSessionAndUpdateCookie(
recent, recent,
password, password,

View File

@@ -15,6 +15,7 @@ export interface SignInWithIDPProps {
children?: ReactNode; children?: ReactNode;
host: string; host: string;
identityProviders: any[]; identityProviders: any[];
authRequestId?: string;
startIDPFlowPath?: (idpId: string) => string; startIDPFlowPath?: (idpId: string) => string;
} }
@@ -24,6 +25,7 @@ const START_IDP_FLOW_PATH = (idpId: string) =>
export function SignInWithIDP({ export function SignInWithIDP({
host, host,
identityProviders, identityProviders,
authRequestId,
startIDPFlowPath = START_IDP_FLOW_PATH, startIDPFlowPath = START_IDP_FLOW_PATH,
}: SignInWithIDPProps) { }: SignInWithIDPProps) {
const [loading, setLoading] = useState<boolean>(false); const [loading, setLoading] = useState<boolean>(false);
@@ -40,7 +42,10 @@ export function SignInWithIDP({
}, },
body: JSON.stringify({ body: JSON.stringify({
idpId, idpId,
successUrl: `${host}/idp/${provider}/success`, successUrl: authRequestId
? `${host}/idp/${provider}/success?` +
new URLSearchParams({ authRequestId })
: `${host}/idp/${provider}/success`,
failureUrl: `${host}/idp/${provider}/failure`, failureUrl: `${host}/idp/${provider}/failure`,
}), }),
}); });

View File

@@ -44,15 +44,15 @@ export async function addSessionToCookie(
currentSessions = [...currentSessions, session]; currentSessions = [...currentSessions, session];
} }
if (cleanup) { // if (cleanup) {
const now = new Date(); // const now = new Date();
const filteredSessions = currentSessions.filter( // const filteredSessions = currentSessions.filter(
(session) => new Date(session.expirationDate) > now // (session) => new Date(session.expirationDate) > now
); // );
return setSessionHttpOnlyCookie(filteredSessions); // return setSessionHttpOnlyCookie(filteredSessions);
} else { // } else {
return setSessionHttpOnlyCookie(currentSessions); return setSessionHttpOnlyCookie(currentSessions);
} // }
} }
export async function updateSessionCookie( export async function updateSessionCookie(
@@ -71,15 +71,15 @@ export async function updateSessionCookie(
if (foundIndex > -1) { if (foundIndex > -1) {
sessions[foundIndex] = session; sessions[foundIndex] = session;
if (cleanup) { // if (cleanup) {
const now = new Date(); // const now = new Date();
const filteredSessions = sessions.filter( // const filteredSessions = sessions.filter(
(session) => new Date(session.expirationDate) > now // (session) => new Date(session.expirationDate) > now
); // );
return setSessionHttpOnlyCookie(filteredSessions); // return setSessionHttpOnlyCookie(filteredSessions);
} else { // } else {
return setSessionHttpOnlyCookie(sessions); return setSessionHttpOnlyCookie(sessions);
} // }
} else { } else {
throw "updateSessionCookie: session id now found"; throw "updateSessionCookie: session id now found";
} }
@@ -97,15 +97,15 @@ export async function removeSessionFromCookie(
: [session]; : [session];
const reducedSessions = sessions.filter((s) => s.id !== session.id); const reducedSessions = sessions.filter((s) => s.id !== session.id);
if (cleanup) { // if (cleanup) {
const now = new Date(); // const now = new Date();
const filteredSessions = reducedSessions.filter( // const filteredSessions = reducedSessions.filter(
(session) => new Date(session.expirationDate) > now // (session) => new Date(session.expirationDate) > now
); // );
return setSessionHttpOnlyCookie(filteredSessions); // return setSessionHttpOnlyCookie(filteredSessions);
} else { // } else {
return setSessionHttpOnlyCookie(reducedSessions); return setSessionHttpOnlyCookie(reducedSessions);
} // }
} }
export async function getMostRecentSessionCookie(): Promise<any> { export async function getMostRecentSessionCookie(): Promise<any> {
@@ -180,12 +180,14 @@ export async function getAllSessionCookieIds(
if (stringifiedCookie?.value) { if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
return sessions // if (cleanup) {
.filter((session) => { // const now = new Date();
const now = new Date(); // return sessions
cleanup ? new Date(session.expirationDate) > now : true; // .filter((session) => new Date(session.expirationDate) > now)
}) // .map((session) => session.id);
.map((session) => session.id); // } else {
return sessions.map((session) => session.id);
// }
} else { } else {
return []; return [];
} }
@@ -204,10 +206,15 @@ export async function getAllSessions(
if (stringifiedCookie?.value) { if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value); const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);
return sessions.filter((session) => {
const now = new Date(); // if (cleanup) {
cleanup ? new Date(session.expirationDate) > now : true; // const now = new Date();
}); // return sessions.filter(
// (session) => new Date(session.expirationDate) > now
// );
// } else {
return sessions;
// }
} else { } else {
return []; return [];
} }