-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathuseResetPassword.ts
60 lines (55 loc) · 1.77 KB
/
useResetPassword.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { OnError, OnSuccess, useAuthProvider, useNotify } from 'ra-core';
import { useMutation, UseMutationResult } from '@tanstack/react-query';
import { ResetPasswordParams, SupabaseAuthProvider } from './authProvider';
/**
* This hook returns a function to call in order to reset a user password on Supabase.
*
* @example
* import { useSupabaseAccessToken } from 'ra-supabase-core';
*
* const ResetPasswordPage = () => {
* const [resetPassword] = useResetPassword();
*
* const handleSubmit = event => {
* resetPassword({
* email: event.currentTarget.elements.email.value,
* });
* };
*
* return (
* <form onSubmit={handleSubmit}>
* <label for="email">Email:</label>
* <input id="email" name="email" type="email" />
* <button type="submit">Reset password</button>
* </form>
* );
* };
**/
export const useResetPassword = (
options?: UseResetPasswordOptions
): [
UseMutationResult<unknown, Error, ResetPasswordParams>['mutate'],
UseMutationResult<unknown, Error, ResetPasswordParams>
] => {
const notify = useNotify();
const authProvider = useAuthProvider<SupabaseAuthProvider>();
const {
onSuccess = () => {
notify('ra-supabase.auth.password_reset', { type: 'info' });
},
onError = error => notify(error.message, { type: 'error' }),
} = options || {};
const mutation = useMutation<unknown, Error, ResetPasswordParams>({
mutationFn: params => {
return authProvider.resetPassword(params);
},
onSuccess,
onError,
retry: false,
});
return [mutation.mutate, mutation];
};
export type UseResetPasswordOptions = {
onSuccess?: OnSuccess;
onError?: OnError;
};