Files
documenso/packages/lib/client-only/hooks/use-password-setup-request.ts
T
Lucas Smith 6a99b40cba fix: improve action auth flow for passwordless users (#3358)
Selecting password auth failed with a generic "Unauthorized" for users
who signed up via OAuth or passkey, with no way to set one.

Detect the missing password and email the existing reset link from the
signing dialog and security settings. Require a 2FA code and rate limit
update-password.
2026-09-11 14:17:34 +10:00

32 lines
1.0 KiB
TypeScript

import { authClient } from '@documenso/auth/client';
import { useMutation } from '@tanstack/react-query';
import { AppError } from '../../errors/app-error';
import { useSession } from '../providers/session';
export type UsePasswordSetupRequestOptions = {
onSuccess?: () => void;
onError?: (errorCode: string) => void;
};
/**
* Sends the signed in user the standard password reset email so they can set a
* password via a verified link, rather than letting a bare session mint one.
*/
export const usePasswordSetupRequest = ({ onSuccess, onError }: UsePasswordSetupRequestOptions = {}) => {
const { user } = useSession();
const { mutate, isPending, isSuccess, error } = useMutation({
mutationFn: async () => authClient.emailPassword.forgotPassword({ email: user.email }),
onSuccess,
onError: (err) => onError?.(AppError.parseError(err).code),
});
return {
requestSetupLink: () => mutate(),
isPending,
isSuccess,
errorCode: error ? AppError.parseError(error).code : null,
};
};