mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
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:
@@ -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' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user