Skip to content

Commit db14902

Browse files
committed
Redirects for already logged-in users in connect-next playground
1 parent 1049a29 commit db14902

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

packages/web-core/src/services/WebAuthnService.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ export class WebAuthnService {
5959

6060
let credential: PublicKeyCredential;
6161
if (conditional) {
62-
const p1 = await navigator.credentials.create({
63-
publicKey,
64-
signal: abortController.signal,
65-
mediation: 'conditional',
66-
} as never);
67-
const p2 = new Promise<null>(resolve => setTimeout(() => resolve(null), 5000));
68-
69-
const result = await Promise.race([p1, p2]);
70-
if (!result) {
71-
throw new Error('Timeout after 5000ms');
72-
}
62+
const result = await WebAuthnService.raceWithTimeout(
63+
navigator.credentials.create({
64+
publicKey,
65+
signal: abortController.signal,
66+
mediation: 'conditional',
67+
} as never),
68+
5000,
69+
);
7370

7471
credential = result as PublicKeyCredential;
7572
} else {
@@ -309,9 +306,7 @@ export class WebAuthnService {
309306
allAcceptedCredentialIds: credentialIds,
310307
});
311308

312-
const p2 = new Promise((_, reject) => setTimeout(() => reject(new Error(`Timeout after 2000ms`)), 2000));
313-
314-
await Promise.race([p1, p2]);
309+
await WebAuthnService.raceWithTimeout(p1, 2000);
315310
} catch (e) {
316311
log.debug('Error calling signalAllAcceptedCredentials', e);
317312
return;
@@ -335,4 +330,12 @@ export class WebAuthnService {
335330
return;
336331
}
337332
}
333+
334+
static async raceWithTimeout<T>(p: Promise<T>, ms: number): Promise<T> {
335+
const timeout = new Promise<never>((_, reject) =>
336+
setTimeout(() => reject(new Error(`timeout of ${ms}ms reached`)), ms),
337+
);
338+
339+
return Promise.race<T>([p, timeout]);
340+
}
338341
}

playground/connect-next/app/(no-auth)/login/ConventionalLogin.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export const ConventionalLogin = ({ initialUserProvidedIdentifier, initialError
4646
}
4747
} catch (e) {
4848
if (e instanceof Error) {
49+
if (e.name === 'UserAlreadyAuthenticatedException') {
50+
router.push('/profile');
51+
}
52+
4953
return e.message;
5054
}
5155

playground/connect-next/app/(no-auth)/page.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ export default function SignupPage() {
3737
console.log(resLogin);
3838
router.push('/post-login?post-signup=true');
3939
} catch (err) {
40-
console.error('Error during signup:', err);
40+
if (err instanceof Error) {
41+
if (err.name === 'UserAlreadyAuthenticatedException') {
42+
router.push('/profile');
43+
return;
44+
}
45+
}
46+
47+
console.error('Unhandled error during signup:', err);
4148
}
4249
};
4350

0 commit comments

Comments
 (0)