login/checkout

This commit is contained in:
2025-06-30 15:27:46 +02:00
parent 012058b998
commit fd4b122936
11 changed files with 336 additions and 212 deletions

View File

@ -3,6 +3,10 @@ import type { Customer } from "~/types/user";
export const useUserStore = defineStore("userStore", () => {
const store = useStore();
const menuStore = useMenuStore();
const checkoutStore = useCheckoutStore();
const { $toast } = useNuxtApp();
const fullUserData = ref<Customer | null>(null);
const isLogged = ref<boolean>(true);
@ -10,28 +14,26 @@ export const useUserStore = defineStore("userStore", () => {
async function checkIsLogged() {
try {
const { data } = await useMyFetch<
GenericResponse<{ loggedin: boolean } | Customer>
>(`/api/public/user`, {
headers: {
"Content-Type": "application/json",
},
onErrorOccured: async (_, status) => {
throw createError({
statusCode: status,
statusMessage: `HTTP error: ${status}`,
});
},
});
const { data } = await useMyFetch<GenericResponse<Customer>>(
`/api/public/user`,
{
headers: {
"Content-Type": "application/json",
},
onErrorOccured: async (_, status) => {
throw createError({
statusCode: status,
statusMessage: `HTTP error: ${status}`,
});
},
}
);
if ("loggedin" in data && data.loggedin === true) {
isLogged.value = true;
user.value = null;
fullUserData.value = null;
} else if ("first_name" in data && "last_name" in data) {
isLogged.value = true;
user.value = `${data.first_name} ${data.last_name}`;
fullUserData.value = data as Customer;
user.value = `${data.first_name} ${data.last_name}` as any;
fullUserData.value = data;
checkoutStore.accountPhoneNumber = fullUserData.value.phone_number;
} else {
isLogged.value = false;
user.value = null;
@ -48,7 +50,6 @@ export const useUserStore = defineStore("userStore", () => {
const vLogin = ref<boolean>(true);
const vCodeVerify = ref<boolean>(false);
const vCode = ref<number | null>(null);
const vEmail = ref<string>("");
async function logIn() {
try {
const data = await useMyFetch<GenericResponse<object>>(
@ -71,35 +72,77 @@ export const useUserStore = defineStore("userStore", () => {
if (data.status === 200 || data.status === 201) {
console.log(vCodeVerify.value);
// $toast.success("Address successfully added", {
// autoClose: 5000,
// dangerouslyHTMLString: true,
// });
$toast.success("Code successfully sent to your email", {
autoClose: 5000,
dangerouslyHTMLString: true,
});
vLogin.value = false;
vCodeVerify.value = true;
} else {
$toast.error("Failed to sent code to your email. Please try again.", {
autoClose: 5000,
dangerouslyHTMLString: true,
});
}
// else {
// $toast.error("Failed to add address. Please try again.", {
// autoClose: 5000,
// dangerouslyHTMLString: true,
// });
// }
store.minValue = data;
} catch (error) {
console.error("getList error:", error);
}
}
const sendFormCode = async (redirect?: boolean) => {
try {
const data = await useMyFetch<GenericResponse<object>>(
`/api/public/user/session/confirm`,
{
method: "POST",
body: JSON.stringify({
code: vCode.value,
mail: email.value,
}),
headers: {
"Content-Type": "application/json",
},
onErrorOccured: (_, status) => {
throw new Error(`HTTP error: ${status}`);
},
}
);
await checkIsLogged();
if (isLogged.value) {
if (redirect) {
console.log(isLogged.value);
menuStore.navigateToItem();
} else {
// window.location.href = atob(redirect);
}
} else {
useNuxtApp().$toast.error(`Error occurred: Failed to confirm code`, {
autoClose: 5000,
dangerouslyHTMLString: true,
});
}
} catch (e) {
useNuxtApp().$toast.error(`Invalid code provided`, {
autoClose: 5000,
dangerouslyHTMLString: true,
});
}
};
return {
isLogged,
user,
fullUserData,
vCodeVerify,
vCode,
vEmail,
email,
password,
logIn,
checkIsLogged,
sendFormCode,
};
});