146 lines
4.0 KiB
JavaScript
146 lines
4.0 KiB
JavaScript
import { defineStore } from "pinia";
|
|
import { fetchWrapper } from "@/api";
|
|
import router from "@/router";
|
|
import { USER, SELECTED_LANG } from "@/helpers";
|
|
import { useAlertStore } from "@/stores";
|
|
|
|
const baseUrl = `${import.meta.env.VITE_API_URL}/api`;
|
|
|
|
export const useAuthStore = defineStore({
|
|
id: "auth",
|
|
state: () => ({
|
|
// initialize state from local storage to enable user to stay logged in
|
|
user: JSON.parse(localStorage.getItem(USER)),
|
|
returnUrl: null,
|
|
emailForVerification: null,
|
|
}),
|
|
actions: {
|
|
async login(email, password) {
|
|
try {
|
|
const lang = localStorage.getItem(SELECTED_LANG);
|
|
|
|
localStorage.clear();
|
|
|
|
localStorage.setItem(SELECTED_LANG, lang);
|
|
|
|
const response = await fetchWrapper.post(`${baseUrl}/login`, {
|
|
email,
|
|
password,
|
|
});
|
|
|
|
// update pinia state
|
|
this.user = response["data"];
|
|
|
|
// store user details and jwt in local storage to keep user logged in between page refreshes
|
|
localStorage.setItem(USER, JSON.stringify(this.user));
|
|
|
|
// redirect to home page
|
|
router.push({ path: "/" });
|
|
} catch (error) {}
|
|
},
|
|
|
|
async logout() {
|
|
try {
|
|
const response = await fetchWrapper.post(`${baseUrl}/logout`);
|
|
console.log("logout response: ", response);
|
|
|
|
localStorage.removeItem(USER);
|
|
this.user = null;
|
|
console.log("this.user: ", this.user);
|
|
router.push({ path: "/login" });
|
|
} catch (error) {
|
|
//console.log("logout err :", error);
|
|
}
|
|
},
|
|
|
|
async register(newUser) {
|
|
try {
|
|
const response = await fetchWrapper.post(
|
|
`${baseUrl}/register`,
|
|
newUser
|
|
);
|
|
|
|
if (response.data && response.data.is_verified) {
|
|
router.push({ path: "/login" });
|
|
} else {
|
|
this.emailForVerification = response.data.email;
|
|
router.push({ path: "/email-verify" });
|
|
}
|
|
////console.warn(response.data);
|
|
//
|
|
|
|
// redirect to Email verification page
|
|
// router.push({ path: "/email-verify" });
|
|
} catch (error) {
|
|
// const alertStore = useAlertStore();
|
|
// alertStore.error(error);
|
|
}
|
|
},
|
|
|
|
async verifyEmail(email, token) {
|
|
try {
|
|
const response = await fetchWrapper.post(`${baseUrl}/verify-email`, {
|
|
email,
|
|
token,
|
|
});
|
|
|
|
// update pinia state
|
|
this.user = response["data"];
|
|
|
|
// store user details and jwt in local storage to keep user logged in between page refreshes
|
|
localStorage.setItem(USER, JSON.stringify(this.user));
|
|
|
|
// redirect to home page
|
|
router.push({ path: "/" });
|
|
} catch (error) {
|
|
// const alertStore = useAlertStore();
|
|
// alertStore.error(error);
|
|
}
|
|
},
|
|
|
|
async updateClient(client) {
|
|
try {
|
|
const response = await fetchWrapper.post(
|
|
`${baseUrl}/update-client`,
|
|
client
|
|
);
|
|
|
|
const updatedClient = response["data"];
|
|
updatedClient["token"] = this.user.token;
|
|
|
|
// update pinia state
|
|
this.user = updatedClient;
|
|
|
|
// store user details and jwt in local storage to keep user logged in between page refreshes
|
|
localStorage.setItem(USER, JSON.stringify(this.user));
|
|
|
|
const alertStore = useAlertStore();
|
|
// trigger alert
|
|
alertStore.success("Successfully updated");
|
|
} catch (error) {
|
|
// const alertStore = useAlertStore();
|
|
// alertStore.error(error);
|
|
}
|
|
},
|
|
|
|
async forgotPassword(email) {
|
|
try {
|
|
const alertStore = useAlertStore();
|
|
|
|
const response = await fetchWrapper.post(`${baseUrl}/forgot-password`, {
|
|
email,
|
|
});
|
|
|
|
//console.log("forgot password", response);
|
|
|
|
alertStore.success(response.message);
|
|
|
|
// redirect to home page
|
|
router.push({ path: "/update-password" });
|
|
} catch (error) {
|
|
// router.push({ path: "/update-password" });
|
|
}
|
|
},
|
|
},
|
|
});
|