From 1d267f7116a630dd317bd03059a14a2c25843699 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Thu, 18 Dec 2025 09:58:00 +0545 Subject: [PATCH] Login and other configurtion for Darta Chalani --- src/core/endpoints/auth.ts | 8 ++-- src/core/endpoints/dartachalani.ts | 2 + src/stores/User/Auth.ts | 66 ++++++++++++++++++++++------- src/stores/User/User.ts | 4 +- src/views/Chalani/CreateChalani.vue | 47 ++++++++++++++------ src/views/Darta/Create.vue | 38 ++++++++++++----- 6 files changed, 120 insertions(+), 45 deletions(-) diff --git a/src/core/endpoints/auth.ts b/src/core/endpoints/auth.ts index fe51fd0..a3cbc60 100644 --- a/src/core/endpoints/auth.ts +++ b/src/core/endpoints/auth.ts @@ -1,7 +1,7 @@ export const authAPI = { - // csrfGenerate: "users/auth/csrf/", - login: "users/login/", - refresh: "users/token/refresh/", - // logout: "users/auth/logout/", + csrfGenerate: "", + login: "auth/token/", + refresh: "auth/token/refresh/", + logout: "auth/logout/", self: "users/self/", }; diff --git a/src/core/endpoints/dartachalani.ts b/src/core/endpoints/dartachalani.ts index e69de29..64236d1 100644 --- a/src/core/endpoints/dartachalani.ts +++ b/src/core/endpoints/dartachalani.ts @@ -0,0 +1,2 @@ +export const departmentAPI = "dartachalani/department/" +export const categoryAPI = "dartachalani/documentcategory/" \ No newline at end of file diff --git a/src/stores/User/Auth.ts b/src/stores/User/Auth.ts index 48ad4ee..6f4eea6 100644 --- a/src/stores/User/Auth.ts +++ b/src/stores/User/Auth.ts @@ -12,7 +12,7 @@ export const useAuth = defineStore("auth", { username: "", password: "", } as LoginPayload, - token: null as null | string, + token: localStorage.getItem("access_token") || (null as null | string), loginError: false, }), @@ -23,22 +23,41 @@ export const useAuth = defineStore("auth", { getLoginError(state) { return state.loginError; }, + isAuthenticated(state){ + return !!state.token + } }, actions: { async csrfGenerate() { - try { - await api.get(authAPI.csrfGenerate); - } catch (error) { - console.error(error); - } + + return; }, async login() { + + const userStore = useUser(); + try { - // const response = await api.post(authAPI.login, this.loginDetails); - await api.post(authAPI.login, this.loginDetails); - Toast.success("Logged in successfully."); - router.push({ name: "dashboard" }); + const response = await api.post(authAPI.login, this.loginDetails); + + const accessToken = response.data.access; + const refreshToken = response.data.refresh + + if(accessToken){ + this.token = accessToken; + localStorage.setItem("access_token", accessToken); + localStorage.setItem("refresh_token", refreshToken); + + api.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`; + + await userStore.fetchUserDetail(); + Toast.success("Logged in Successfully") + router.push({name: "dashboard"}) + } + + // await api.post(authAPI.login, this.loginDetails); + // Toast.success("Logged in successfully."); + // router.push({ name: "dashboard" }); } catch { Toast.error("Unable to login!"); this.hasLoginError(); @@ -47,25 +66,40 @@ export const useAuth = defineStore("auth", { async logout() { const userStore = useUser(); try { - await api.post(authAPI.logout); + // await api.post(authAPI.logout); userStore.$reset(); this.loginDetails.username = ""; this.loginDetails.password = ""; this.token = null; + + localStorage.removeItem("access_token") + localStorage.removeItem("refresh_token") + delete api.defaults.headers.common["Authorization"]; Toast.success("Logged out successfully."); router.push({ name: "login" }); } catch (error) { console.error(error); + localStorage.clear() + router.push({ name: "login" }); } }, async refreshToken() { try { - await api.post(authAPI.refresh); - // if (response.data) { - // this.token = response.data.data.access; - // } - return true; + const refresh = localStorage.getItem("refresh_token"); + if (!refresh) return false; + + const response = await api.post(authAPI.refresh, { refresh: refresh}); + + if (response.data && response.data.access){ + const newAccess = response.data.access + this.token = newAccess + localStorage.setItem("access_token", newAccess); + api.defaults.headers.common["Authorization"] = `Bearer ${newAccess}`; + return true; + } + return false; } catch { + this.logout(); return false; } }, diff --git a/src/stores/User/User.ts b/src/stores/User/User.ts index 36f40a8..7e24a30 100644 --- a/src/stores/User/User.ts +++ b/src/stores/User/User.ts @@ -17,8 +17,8 @@ export const useUser = defineStore("user", { actions: { async fetchUserDetail() { try { - // const response = await api.get(authAPI.self); - // this.user = response.data.data; + const response = await api.get(authAPI.self); + this.user = response.data.data; this.isAuthenticated = true; } catch (error: any) { if (error.response.status == 403) { diff --git a/src/views/Chalani/CreateChalani.vue b/src/views/Chalani/CreateChalani.vue index 4bda12c..8fbb186 100644 --- a/src/views/Chalani/CreateChalani.vue +++ b/src/views/Chalani/CreateChalani.vue @@ -19,6 +19,7 @@ label="name" track-by="id" placeholder="Select Department" + :loading="isDepartmentLoading" />
@@ -28,7 +29,7 @@
@@ -160,15 +162,25 @@ + +onMounted(()=>{ + departmentStore.fetchList(departmentAPI, {}, isDepartmentLoading); + categoryStore.fetchList(categoryAPI, {}, isCategoryLoading); +}) + +onBeforeMount(()=>{ + departmentStore.$reset(); + departmentStore.$dispose(); + categoryStore.$reset(); + categoryStore.$dispose(); +}) + \ No newline at end of file diff --git a/src/views/Darta/Create.vue b/src/views/Darta/Create.vue index 9898fa7..824c85e 100644 --- a/src/views/Darta/Create.vue +++ b/src/views/Darta/Create.vue @@ -19,6 +19,7 @@ label="name" track-by="id" placeholder="Select Department" + :loading="isDepartmentLoading" />
@@ -28,13 +29,14 @@
@@ -145,14 +147,25 @@