Skip to content

Commit

Permalink
redirect to login with passkey after register with passkey
Browse files Browse the repository at this point in the history
  • Loading branch information
peintnermax committed Apr 3, 2024
1 parent 836df9e commit 0a1219b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
26 changes: 22 additions & 4 deletions apps/login/app/(login)/passkey/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import Alert from "#/ui/Alert";
import DynamicTheme from "#/ui/DynamicTheme";
import LoginPasskey from "#/ui/LoginPasskey";
import UserAvatar from "#/ui/UserAvatar";
import { getMostRecentCookieWithLoginname } from "#/utils/cookies";
import {
getMostRecentCookieWithLoginname,
getSessionCookieById,
} from "#/utils/cookies";

const title = "Authenticate with a passkey";
const description =
Expand All @@ -14,11 +17,17 @@ export default async function Page({
}: {
searchParams: Record<string | number | symbol, string | undefined>;
}) {
const { loginName, altPassword, authRequestId, organization } = searchParams;
const { loginName, altPassword, authRequestId, organization, sessionId } =
searchParams;

const sessionFactors = await loadSession(loginName, organization);
const sessionFactors = sessionId
? await loadSessionById(sessionId, organization)
: await loadSessionByLoginname(loginName, organization);

async function loadSession(loginName?: string, organization?: string) {
async function loadSessionByLoginname(
loginName?: string,
organization?: string
) {
const recent = await getMostRecentCookieWithLoginname(
loginName,
organization
Expand All @@ -30,6 +39,15 @@ export default async function Page({
});
}

async function loadSessionById(sessionId: string, organization?: string) {
const recent = await getSessionCookieById(sessionId, organization);
return getSession(server, recent.id, recent.token).then((response) => {
if (response?.session) {
return response.session;
}
});
}

const branding = await getBrandingSettings(server, organization);

return (
Expand Down
6 changes: 4 additions & 2 deletions apps/login/ui/RegisterPasskey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,12 @@ export default function RegisterPasskey({
}

if (authRequestId) {
params.set("authRequest", authRequestId);
params.set("authRequestId", authRequestId);
params.set("sessionId", sessionId);
// params.set("altPassword", ${false}); // without setting altPassword this does not allow password
// params.set("loginName", resp.loginName);

router.push("/login?" + params);
router.push("/passkey/login?" + params);
} else {
router.push("/accounts?" + params);
}
Expand Down
11 changes: 9 additions & 2 deletions apps/login/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,21 @@ export async function getMostRecentSessionCookie(): Promise<any> {
}
}

export async function getSessionCookieById(id: string): Promise<SessionCookie> {
export async function getSessionCookieById(
id: string,
organization?: string
): Promise<SessionCookie> {
const cookiesList = cookies();
const stringifiedCookie = cookiesList.get("sessions");

if (stringifiedCookie?.value) {
const sessions: SessionCookie[] = JSON.parse(stringifiedCookie?.value);

const found = sessions.find((s) => s.id === id);
const found = sessions.find((s) =>
organization
? s.organization === organization && s.id === id
: s.id === id
);
if (found) {
return found;
} else {
Expand Down

0 comments on commit 0a1219b

Please sign in to comment.