Files
myeasycms-v2/packages/supabase/src/hooks/use-link-identity-with-email-password.ts
Giancarlo Buomprisco 9033155fcd Add OTP sign-in option + Account Linking (#276)
* feat(accounts): allow linking email password
* feat(auth): add OTP sign-in
* refactor(accounts): remove 'sonner' dependency and update toast imports
* feat(supabase): enable analytics and configure database seeding
* feat(auth): update email templates and add OTP template
* feat(auth): add last sign in method hints
* feat(config): add devIndicators position to bottom-right
* feat(auth): implement comprehensive last authentication method tracking tests
2025-06-13 17:47:35 +08:00

51 lines
1.2 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useSupabase } from './use-supabase';
import { USER_IDENTITIES_QUERY_KEY } from './use-user-identities';
interface Credentials {
email: string;
password: string;
redirectTo: string;
}
export function useLinkIdentityWithEmailPassword() {
const client = useSupabase();
const queryClient = useQueryClient();
const mutationKey = ['auth', 'link-email-password'];
const mutationFn = async (credentials: Credentials) => {
const { email, password, redirectTo } = credentials;
const { error } = await client.auth.updateUser(
{
email,
password,
data: {
// This is used to indicate that the user has a password set
// because Supabase does not add the identity after setting a password
// if the user was created with oAuth
hasPassword: true,
},
},
{ emailRedirectTo: redirectTo },
);
if (error) {
throw error.message ?? error;
}
await client.auth.refreshSession();
};
return useMutation({
mutationKey,
mutationFn,
onSuccess: () => {
return queryClient.invalidateQueries({
queryKey: USER_IDENTITIES_QUERY_KEY,
});
},
});
}