@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
123 lines (122 loc) • 5.02 kB
JavaScript
import { toComparableTimestamp } from "../shared/date-utils.js";
import { isAuthError } from "../shared/medusa-errors.js";
class MedusaRegistrationSignInError extends Error {
code = "registration_sign_in_failed";
email;
reason;
constructor(email, reason){
super("Customer account was created, but automatic sign-in failed. Please sign in again.");
this.name = "MedusaRegistrationSignInError";
this.email = email;
this.reason = reason;
}
}
const defaultReportLogoutError = (error, context)=>{
let message = "[storefront-data/auth] Failed to cleanup auth session after register error.";
if ("logout" === context) message = "[storefront-data/auth] Failed to logout customer session.";
else if ("register-signin-recovery" === context) message = "[storefront-data/auth] Failed to cleanup auth session after registration sign-in recovery error.";
console.warn(message, error);
};
function createMedusaAuthService(sdk, config) {
const reportLogoutError = (error, context)=>{
if (config?.onLogoutError) try {
config.onLogoutError(error, context);
return;
} catch {}
try {
defaultReportLogoutError(error, context);
} catch {}
};
const cleanupRegisterSession = async (context)=>{
try {
await sdk.auth.logout();
} catch (logoutError) {
if (isAuthError(logoutError)) return;
reportLogoutError(logoutError, context);
}
};
const refreshRegisterSession = async (loginToken)=>{
const sessionToken = await sdk.auth.refresh({
Authorization: `Bearer ${loginToken}`
});
if ("string" != typeof sessionToken) throw new Error("Multi-step authentication not supported");
return sessionToken;
};
return {
confirmAccountDeactivation (input) {
return sdk.client.fetch("/store/customers/deactivate/confirm", {
method: "POST",
body: input
});
},
requestAccountDeactivation () {
return sdk.client.fetch("/store/customers/me/deactivate", {
method: "POST",
body: {
confirm: true
}
});
},
async getCustomer (signal) {
try {
const { customer } = await sdk.client.fetch("/store/customers/me", {
signal
});
if (!customer) return null;
if (customer.addresses?.length) customer.addresses = [
...customer.addresses
].sort((a, b)=>toComparableTimestamp(a.created_at) - toComparableTimestamp(b.created_at));
return customer;
} catch (error) {
if (isAuthError(error)) return null;
throw error;
}
},
async login (credentials) {
const token = await sdk.auth.login("customer", "emailpass", credentials);
if ("string" != typeof token) throw new Error("Multi-step authentication not supported");
return token;
},
async logout () {
try {
await sdk.auth.logout();
} catch (error) {
if (isAuthError(error)) return;
reportLogoutError(error, "logout");
throw error;
}
},
async register (data) {
const registrationToken = await sdk.auth.register("customer", "emailpass", {
email: data.email,
password: data.password
});
let customerCreated = false;
try {
if ("string" != typeof registrationToken) throw new Error("Multi-step authentication not supported");
const loginToken = await sdk.auth.login("customer", "emailpass", {
email: data.email,
password: data.password
});
if ("string" != typeof loginToken) throw new Error("Multi-step authentication not supported");
await sdk.store.customer.create({
email: data.email,
first_name: data.first_name,
last_name: data.last_name
});
customerCreated = true;
return await refreshRegisterSession(loginToken);
} catch (err) {
const logoutContext = customerCreated ? "register-signin-recovery" : "register-cleanup";
await cleanupRegisterSession(logoutContext);
if (customerCreated) throw new MedusaRegistrationSignInError(data.email, err);
throw err;
}
},
async updateCustomer (data) {
const { customer } = await sdk.store.customer.update(data);
return customer;
}
};
}
export { MedusaRegistrationSignInError, createMedusaAuthService };