feat(MED-100): create medusa store account for user

This commit is contained in:
2025-07-17 10:19:27 +03:00
parent 736194bb0b
commit 55869ea16f
6 changed files with 110 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ import type { SignInWithPasswordCredentials } from '@supabase/supabase-js';
import { useMutation } from '@tanstack/react-query';
import { medusaLoginOrRegister } from '../../../features/medusa-storefront/src/lib/data/customer';
import { useSupabase } from './use-supabase';
export function useSignInWithEmailPassword() {
@@ -18,11 +19,22 @@ export function useSignInWithEmailPassword() {
const user = response.data?.user;
const identities = user?.identities ?? [];
// if the user has no identities, it means that the email is taken
if (identities.length === 0) {
throw new Error('User already registered');
}
if ('email' in credentials) {
try {
await medusaLoginOrRegister({
email: credentials.email,
password: credentials.password,
});
} catch (error) {
await client.auth.signOut();
throw error;
}
}
return response.data;
};

View File

@@ -1,6 +1,7 @@
import { useMutation } from '@tanstack/react-query';
import { useSupabase } from './use-supabase';
import { medusaLoginOrRegister } from '../../../features/medusa-storefront/src/lib/data/customer';
interface Credentials {
personalCode: string;
@@ -41,6 +42,18 @@ export function useSignUpWithEmailAndPassword() {
throw new Error('User already registered');
}
if ('email' in credentials) {
try {
await medusaLoginOrRegister({
email: credentials.email,
password: credentials.password,
});
} catch (error) {
await client.auth.signOut();
throw error;
}
}
return response.data;
};