Fixed the Customer Context. Because of how setState worked, customers were not properly registered.
Customer Profile Page, Customer Addresses Page, Checkout Page
This commit is contained in:
@ -5,9 +5,11 @@ import React, { createContext, useContext, useEffect, useState } from 'react';
|
||||
|
||||
interface CustomerContextType {
|
||||
customer: HttpTypes.StoreCustomer | undefined;
|
||||
setCustomer: React.Dispatch<React.SetStateAction<HttpTypes.StoreCustomer | undefined>>;
|
||||
register: (firstName: string, lastName: string, email: string, password: string) => Promise<void>;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
logout: () => void;
|
||||
token: string | null;
|
||||
}
|
||||
|
||||
const CustomerContext = createContext<CustomerContextType | null>(null);
|
||||
@ -18,13 +20,12 @@ interface CustomerProviderProps {
|
||||
|
||||
export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
const [customer, setCustomer] = useState<HttpTypes.StoreCustomer | undefined>();
|
||||
const [jwt, setJwt] = useState<string | null>();
|
||||
const [jwt, setJwt] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const storedToken = localStorage.getItem('jwt');
|
||||
const storedCustomer = localStorage.getItem('user');
|
||||
|
||||
console.log('Stored customer: ', storedCustomer);
|
||||
if (storedToken) {
|
||||
setJwt(storedToken);
|
||||
}
|
||||
@ -37,20 +38,16 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
useEffect(() => {
|
||||
if (customer) {
|
||||
localStorage.setItem('user', JSON.stringify(customer));
|
||||
console.log('User set to: ', customer);
|
||||
} else {
|
||||
localStorage.removeItem('user');
|
||||
console.log('User removed');
|
||||
}
|
||||
}, [customer]);
|
||||
|
||||
useEffect(() => {
|
||||
if (jwt) {
|
||||
localStorage.setItem('jwt', jwt);
|
||||
console.log('Jwt set to: ', jwt);
|
||||
} else {
|
||||
localStorage.removeItem('jwt');
|
||||
console.log('Jwt removed');
|
||||
}
|
||||
}, [jwt]);
|
||||
|
||||
@ -66,12 +63,13 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
}
|
||||
|
||||
// obtain JWT token
|
||||
await getJwt(email, password);
|
||||
const token = await getJwt(email, password);
|
||||
|
||||
// get customer
|
||||
await getCustomer();
|
||||
const customer = await getCustomer(token);
|
||||
|
||||
console.log('Logged in');
|
||||
setJwt(token);
|
||||
setCustomer(customer);
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
@ -98,12 +96,19 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
}
|
||||
|
||||
// obtain JWT token
|
||||
await registerEmail(email, password);
|
||||
let token = await registerEmail(email, password);
|
||||
|
||||
// get customer
|
||||
await createCustomer(firstName, lastName, email);
|
||||
let customer = await createCustomer(firstName, lastName, email, token);
|
||||
|
||||
console.log('Logged in');
|
||||
// obtain updated JWT token
|
||||
token = await getJwt(email, password);
|
||||
|
||||
// get customer
|
||||
customer = await getCustomer(token);
|
||||
|
||||
setJwt(token);
|
||||
setCustomer(customer);
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
@ -138,7 +143,7 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
if (!token) {
|
||||
throw new Error('Unable to retrieve token');
|
||||
}
|
||||
setJwt(token);
|
||||
return token;
|
||||
} catch (error) {
|
||||
console.error('Login error in getJwt: ', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
@ -163,47 +168,41 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
if (!token) {
|
||||
throw new Error('Unable to retrieve token');
|
||||
}
|
||||
setJwt(token);
|
||||
return token;
|
||||
} catch (error) {
|
||||
console.error('Register error in registerEmail: ', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
throw error; // Re-throw to allow caller to handle it
|
||||
}
|
||||
};
|
||||
const getCustomer = async () => {
|
||||
const getCustomer = async (token: string) => {
|
||||
try {
|
||||
if (!jwt) {
|
||||
return;
|
||||
}
|
||||
// get customer
|
||||
const response = await fetch(`${process.env.NEXT_PUBLIC_STORE_URL}/store/customers/me`, {
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
Authorization: `Bearer ${token}`,
|
||||
'x-publishable-api-key': process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY as string,
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
|
||||
setCustomer(response.customer);
|
||||
return response.customer;
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
throw error; // Re-throw to allow caller to handle it
|
||||
}
|
||||
};
|
||||
const createCustomer = async (firstName: string, lastName: string, email: string) => {
|
||||
const createCustomer = async (firstName: string, lastName: string, email: string, token: string) => {
|
||||
try {
|
||||
if (!jwt) {
|
||||
return;
|
||||
}
|
||||
// create customer
|
||||
const response = await fetch(`${process.env.NEXT_PUBLIC_STORE_URL}/store/customers`, {
|
||||
credentials: 'include',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${jwt}`,
|
||||
Authorization: `Bearer ${token}`,
|
||||
'x-publishable-api-key': process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY as string,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
@ -213,7 +212,7 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
}),
|
||||
}).then((res) => res.json());
|
||||
|
||||
setCustomer(response.customer);
|
||||
return response.customer;
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
// Optionally, handle error (e.g., show error message to user)
|
||||
@ -224,9 +223,11 @@ export const CustomerProvider = ({ children }: CustomerProviderProps) => {
|
||||
<CustomerContext.Provider
|
||||
value={{
|
||||
customer,
|
||||
setCustomer,
|
||||
register,
|
||||
login,
|
||||
logout,
|
||||
token: jwt,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
|
||||
Reference in New Issue
Block a user