feat(auth): add passkey sign-in and management
Sign in with Face ID, Touch ID, Windows Hello or a security key, keeping email + password as the primary method. - API: BetterAuth passkey plugin deriving rpID/origin from WEB_APP_URL (the browser origin), with residentKey "required" because sign-in is username-less. New Passkey model mapped to the passkeys table. - Web: conditional UI in the email field plus an explicit "Entrar con passkey" button; cancelled ceremonies are ignored. - Settings: create, rename and delete passkeys with TanStack Query. - Pin @better-auth/passkey to 1.7.7: its peers lock better-call, so newer versions break the API.
This commit is contained in:
76
apps/web/src/hooks/use-passkeys.ts
Normal file
76
apps/web/src/hooks/use-passkeys.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { queryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
|
||||
import { authClient } from '@/lib/auth-client'
|
||||
|
||||
export type Passkey = NonNullable<
|
||||
Awaited<ReturnType<typeof authClient.passkey.listUserPasskeys>>['data']
|
||||
>[number]
|
||||
|
||||
const PASSKEYS_QUERY_KEY = ['passkeys'] as const
|
||||
|
||||
export const passkeysQueryOptions = () =>
|
||||
queryOptions({
|
||||
queryKey: PASSKEYS_QUERY_KEY,
|
||||
queryFn: async () => {
|
||||
const { data, error } = await authClient.passkey.listUserPasskeys()
|
||||
|
||||
if (error) throw new Error(error.message ?? 'No pudimos cargar tus passkeys.')
|
||||
|
||||
return data ?? []
|
||||
},
|
||||
})
|
||||
|
||||
export function usePasskeys() {
|
||||
return useQuery(passkeysQueryOptions())
|
||||
}
|
||||
|
||||
function toError(error: { message?: string } | null, fallback: string) {
|
||||
return new Error(error?.message ?? fallback)
|
||||
}
|
||||
|
||||
export function useAddPasskey() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (name: string) => {
|
||||
const { data, error } = await authClient.passkey.addPasskey({ name })
|
||||
|
||||
if (error) throw toError(error, 'No pudimos crear la passkey.')
|
||||
|
||||
return data
|
||||
},
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: PASSKEYS_QUERY_KEY })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdatePasskey() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, name }: { id: string; name: string }) => {
|
||||
const { error } = await authClient.passkey.updatePasskey({ id, name })
|
||||
|
||||
if (error) throw toError(error, 'No pudimos renombrar la passkey.')
|
||||
},
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: PASSKEYS_QUERY_KEY })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeletePasskey() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
const { error } = await authClient.passkey.deletePasskey({ id })
|
||||
|
||||
if (error) throw toError(error, 'No pudimos eliminar la passkey.')
|
||||
},
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: PASSKEYS_QUERY_KEY })
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user