mirror of
https://github.com/documenso/documenso.git
synced 2026-10-03 10:13:52 +10:00
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.
32 lines
1.0 KiB
TypeScript
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,
|
|
};
|
|
};
|