This commit is contained in:
2025-09-28 07:26:46 +03:00
parent e4fcafa57c
commit 70d5b78ca8
9 changed files with 129 additions and 20 deletions

View File

@@ -288,6 +288,20 @@ async function medusaLogin(email: string, password: string) {
return customer.id;
}
export async function medusaResetPassword({
email,
password,
}: {
email: string;
password: string;
}) {
await sdk.auth.resetPassword('customer', 'emailpass', { identifier: email });
// await sdk.auth.updateProvider("customer", "emailpass", {
// email,
// password,
// }, token)
}
async function medusaRegister({
email,
password,
@@ -321,6 +335,10 @@ async function medusaRegister({
);
}
export async function medusaRefreshSession() {
await sdk.auth.refresh();
}
export async function medusaLoginOrRegister(
credentials: {
email: string;
@@ -343,7 +361,16 @@ export async function medusaLoginOrRegister(
})();
try {
return await medusaLogin(email, password);
try {
await medusaResetPassword({ email, password });
return await medusaLogin(email, password);
} catch (loginError) {
if ((loginError as Error)?.message?.includes('Invalid email or password')) {
await medusaResetPassword({ email, password });
return await medusaLogin(email, password);
}
throw loginError;
}
} catch (loginError) {
console.error(
'Failed to login customer, attempting to register',

View File

@@ -17,6 +17,7 @@ export default function medusaError(error: any): never {
throw new Error('No response received: ' + error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error('Error setting up the request:', error);
throw new Error('Error setting up the request: ' + error.message);
}
}