Skip to content

Commit

Permalink
Don't automatically poke for a Passkey
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed Dec 30, 2023
1 parent c13c2fb commit ff843d6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/app/[locale]/(public)/account/create-passkey/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Label } from '@/components/ui/label';
import { useSurreal } from '@/lib/Surreal';
import { useAuth } from '@/lib/auth';
import { useFeatureFlags } from '@/lib/featureFlags';
import { useRegisterPasskey } from '@/lib/webauthn';
import { useAutoPoke, useRegisterPasskey } from '@/lib/webauthn';
import { Link, useRouter } from '@/locales/navigation';
import { Credential } from '@/schema/resources/credential';
import { zodResolver } from '@hookform/resolvers/zod';
Expand Down Expand Up @@ -69,10 +69,17 @@ function CreatePasskey({
}: Pick<ReturnType<typeof useRegisterPasskey>, 'loading' | 'register'> & {
signup?: boolean;
}) {
const [_, setAutoPoke] = useAutoPoke();
const t = useTranslations('pages.account.create-passkey.pre');
return (
<CardFooter className="space-x-4">
<Button onClick={() => register()} disabled={loading}>
<Button
onClick={() => {
register();
setAutoPoke(true);
}}
disabled={loading}
>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{t('trigger')}
</Button>
Expand Down
11 changes: 8 additions & 3 deletions src/app/[locale]/(public)/account/signin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useFeatureFlags } from '@/lib/featureFlags';
import { cn } from '@/lib/utils';
import { usePasskeyAuthentication } from '@/lib/webauthn';
import { useAutoPoke, usePasskeyAuthentication } from '@/lib/webauthn';
import { useRouter } from '@/locales/navigation';
import { zodResolver } from '@hookform/resolvers/zod';
import { ChevronsUpDown, Info, KeyRound, Loader2, XCircle } from 'lucide-react';
Expand All @@ -44,11 +44,13 @@ export default function Signin() {
(localStorage.getItem('signin.default-scope') as scope)) ||
'user';

const [autoPoke, setAutoPoke] = useAutoPoke();

const {
loading: passkeyLoading,
authenticate: tryPasskey,
passkey,
} = usePasskeyAuthentication();
} = usePasskeyAuthentication({ autoPoke });
const [featureFlags] = useFeatureFlags();
const router = useRouter();
const [navigating, setNavigating] = useState(false);
Expand Down Expand Up @@ -223,7 +225,10 @@ export default function Signin() {
<Button
type="button"
className={passkey ? ' bg-green-500' : ''}
onClick={() => tryPasskey()}
onClick={() => {
tryPasskey();
setAutoPoke(true);
}}
variant={passkey ? 'default' : 'outline'}
disabled={passkeyLoading}
>
Expand Down
43 changes: 29 additions & 14 deletions src/lib/webauthn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { client } from '@passwordless-id/webauthn';
import { useMutation } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { z } from 'zod';
import { useSurreal } from './Surreal';
import { useAuth } from './auth';
Expand Down Expand Up @@ -59,11 +59,32 @@ export function useWebAuthnAvailable() {
return available;
}

export function useAutoPoke() {
const [autoPoke, setAutoPoke] = useState(false);

const updatePreference = useCallback(
(autoPoke: boolean, persist = true) => {
setAutoPoke(autoPoke);
if (persist && typeof window !== 'undefined') {
localStorage.setItem('auto-poke', autoPoke.toString());
}
},
[setAutoPoke]
);

useEffect(() => {
if (typeof window !== 'undefined') {
const state = localStorage.getItem('auto-poke');
setAutoPoke(state == 'true');
}
}, [setAutoPoke]);

return [autoPoke, updatePreference] as const;
}

export function useRegisterPasskey() {
const [featureFlags] = useFeatureFlags();
const [didPoke, setDidPoke] = useState(false);
const { user, loading: userLoading } = useAuth();
const ready = useReadyAfter(10);

const {
isPending: isRegistering,
Expand Down Expand Up @@ -127,18 +148,12 @@ export function useRegisterPasskey() {
});

const loading = userLoading || isRegistering;

useEffect(() => {
if (ready && !loading && user && !didPoke) {
register();
setDidPoke(true);
}
}, [ready, loading, user, register, didPoke, setDidPoke]);

return { loading, register, passkey };
}

export function usePasskeyAuthentication() {
export function usePasskeyAuthentication({
autoPoke,
}: { autoPoke?: boolean } = {}) {
const [featureFlags] = useFeatureFlags();
const [didPoke, setDidPoke] = useState(false);
const { refreshUser } = useAuth();
Expand Down Expand Up @@ -215,11 +230,11 @@ export function usePasskeyAuthentication() {
});

useEffect(() => {
if (ready && !loading && !didPoke) {
if (ready && autoPoke && !loading && !didPoke) {
authenticate();
setDidPoke(true);
}
}, [ready, loading, authenticate, didPoke, setDidPoke]);
}, [ready, autoPoke, loading, authenticate, didPoke, setDidPoke]);

return { loading, authenticate, passkey };
}

0 comments on commit ff843d6

Please sign in to comment.