fix(native): the handoff's passkey sign-in lives in native/browserSignIn, so the page hook reaches no API on its own

This commit is contained in:
Maurice
2026-11-08 18:33:00 +01:00
parent cd1f39377d
commit afb6eae4c8
4 changed files with 32 additions and 25 deletions
+16
View File
@@ -1,6 +1,8 @@
import { sha256 } from '@noble/hashes/sha2';
import { bytesToHex } from '@noble/hashes/utils';
import { startAuthentication } from '@simplewebauthn/browser';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { authApi } from '../api/client';
import { nativeAuthApi } from '../api/nativeAuth';
import {
BrowserSignInCancelled,
@@ -9,11 +11,14 @@ import {
rememberHandoffReturn,
requestHandoffCode,
signInThroughBrowser,
signInWithPasskey,
takeHandoffReturn,
} from './browserSignIn';
import { TrekShell } from './trekShell';
vi.mock('./trekShell', () => ({ APP_URL_SCHEME: 'com.liketrek.trek', TrekShell: { authenticate: vi.fn() } }));
vi.mock('@simplewebauthn/browser', () => ({ startAuthentication: vi.fn() }));
vi.mock('../api/client', () => ({ authApi: { passkey: { loginOptions: vi.fn(), loginVerify: vi.fn() } } }));
vi.mock('../api/nativeAuth', () => ({ nativeAuthApi: { exchange: vi.fn(), handoff: vi.fn() } }));
beforeEach(() => {
@@ -110,3 +115,14 @@ describe('the remembered handoff return', () => {
expect(takeHandoffReturn()).toBeNull();
});
});
describe('signInWithPasskey', () => {
it('asks for the passkey and has the server verify it', async () => {
vi.mocked(authApi.passkey.loginOptions).mockResolvedValue({ challenge: 'x' });
vi.mocked(startAuthentication).mockResolvedValue({ id: 'cred' } as never);
vi.mocked(authApi.passkey.loginVerify).mockResolvedValue({ token: '', user: {} });
await signInWithPasskey();
expect(startAuthentication).toHaveBeenCalledWith({ optionsJSON: { challenge: 'x' } });
expect(authApi.passkey.loginVerify).toHaveBeenCalledWith({ id: 'cred' });
});
});
+9
View File
@@ -1,5 +1,7 @@
import { sha256 } from '@noble/hashes/sha2';
import { bytesToHex } from '@noble/hashes/utils';
import { startAuthentication } from '@simplewebauthn/browser';
import { authApi } from '../api/client';
import { nativeAuthApi } from '../api/nativeAuth';
import { APP_URL_SCHEME, TrekShell } from './trekShell';
@@ -76,6 +78,13 @@ export async function requestHandoffCode(challenge: string): Promise<string> {
return code;
}
/** The browser side of a passkey sign-in the app started: the prompt, then the session cookie. */
export async function signInWithPasskey(): Promise<void> {
const options = await authApi.passkey.loginOptions();
const assertion = await startAuthentication({ optionsJSON: options });
await authApi.passkey.loginVerify(assertion);
}
export class BrowserSignInCancelled extends Error {
constructor() {
super('Sign-in cancelled');