Compare commits
4 Commits
cd22e15d94
...
8bdb293d9d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8bdb293d9d | ||
|
|
fe10d389b3 | ||
|
|
78c4e44791 | ||
|
|
1d267f7116 |
|
|
@ -1,7 +1,7 @@
|
||||||
export const authAPI = {
|
export const authAPI = {
|
||||||
// csrfGenerate: "users/auth/csrf/",
|
csrfGenerate: "",
|
||||||
login: "users/login/",
|
login: "auth/token/",
|
||||||
refresh: "users/token/refresh/",
|
refresh: "auth/token/refresh/",
|
||||||
// logout: "users/auth/logout/",
|
logout: "auth/logout/",
|
||||||
self: "users/self/",
|
self: "users/self/",
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
export const departmentAPI = "dartachalani/department/"
|
||||||
|
export const categoryAPI = "dartachalani/documentcategory/"
|
||||||
|
|
@ -12,7 +12,7 @@ export const useAuth = defineStore("auth", {
|
||||||
username: "",
|
username: "",
|
||||||
password: "",
|
password: "",
|
||||||
} as LoginPayload,
|
} as LoginPayload,
|
||||||
token: null as null | string,
|
token: localStorage.getItem("access_token") || (null as null | string),
|
||||||
loginError: false,
|
loginError: false,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
@ -23,22 +23,41 @@ export const useAuth = defineStore("auth", {
|
||||||
getLoginError(state) {
|
getLoginError(state) {
|
||||||
return state.loginError;
|
return state.loginError;
|
||||||
},
|
},
|
||||||
|
isAuthenticated(state){
|
||||||
|
return !!state.token
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
async csrfGenerate() {
|
async csrfGenerate() {
|
||||||
try {
|
|
||||||
await api.get(authAPI.csrfGenerate);
|
return;
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async login() {
|
async login() {
|
||||||
|
|
||||||
|
const userStore = useUser();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const response = await api.post(authAPI.login, this.loginDetails);
|
const response = await api.post(authAPI.login, this.loginDetails);
|
||||||
await api.post(authAPI.login, this.loginDetails);
|
|
||||||
Toast.success("Logged in successfully.");
|
const accessToken = response.data.access;
|
||||||
router.push({ name: "dashboard" });
|
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 {
|
} catch {
|
||||||
Toast.error("Unable to login!");
|
Toast.error("Unable to login!");
|
||||||
this.hasLoginError();
|
this.hasLoginError();
|
||||||
|
|
@ -47,25 +66,40 @@ export const useAuth = defineStore("auth", {
|
||||||
async logout() {
|
async logout() {
|
||||||
const userStore = useUser();
|
const userStore = useUser();
|
||||||
try {
|
try {
|
||||||
await api.post(authAPI.logout);
|
// await api.post(authAPI.logout);
|
||||||
userStore.$reset();
|
userStore.$reset();
|
||||||
this.loginDetails.username = "";
|
this.loginDetails.username = "";
|
||||||
this.loginDetails.password = "";
|
this.loginDetails.password = "";
|
||||||
this.token = null;
|
this.token = null;
|
||||||
|
|
||||||
|
localStorage.removeItem("access_token")
|
||||||
|
localStorage.removeItem("refresh_token")
|
||||||
|
delete api.defaults.headers.common["Authorization"];
|
||||||
Toast.success("Logged out successfully.");
|
Toast.success("Logged out successfully.");
|
||||||
router.push({ name: "login" });
|
router.push({ name: "login" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
localStorage.clear()
|
||||||
|
router.push({ name: "login" });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async refreshToken() {
|
async refreshToken() {
|
||||||
try {
|
try {
|
||||||
await api.post(authAPI.refresh);
|
const refresh = localStorage.getItem("refresh_token");
|
||||||
// if (response.data) {
|
if (!refresh) return false;
|
||||||
// this.token = response.data.data.access;
|
|
||||||
// }
|
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 true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
} catch {
|
} catch {
|
||||||
|
this.logout();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@ export const useUser = defineStore("user", {
|
||||||
actions: {
|
actions: {
|
||||||
async fetchUserDetail() {
|
async fetchUserDetail() {
|
||||||
try {
|
try {
|
||||||
// const response = await api.get(authAPI.self);
|
const response = await api.get(authAPI.self);
|
||||||
// this.user = response.data.data;
|
this.user = response.data.data;
|
||||||
this.isAuthenticated = true;
|
this.isAuthenticated = true;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.response.status == 403) {
|
if (error.response.status == 403) {
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
label="name"
|
label="name"
|
||||||
track-by="id"
|
track-by="id"
|
||||||
placeholder="Select Department"
|
placeholder="Select Department"
|
||||||
|
:loading="isDepartmentLoading"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="">
|
<div class="">
|
||||||
|
|
@ -28,7 +29,7 @@
|
||||||
</label>
|
</label>
|
||||||
<Multiselect
|
<Multiselect
|
||||||
v-model="formData.documentCategory"
|
v-model="formData.documentCategory"
|
||||||
:options="documentCategories"
|
:options="categories"
|
||||||
:multiple="false"
|
:multiple="false"
|
||||||
:searchable="true"
|
:searchable="true"
|
||||||
:allow-empty="false"
|
:allow-empty="false"
|
||||||
|
|
@ -36,6 +37,7 @@
|
||||||
label="name"
|
label="name"
|
||||||
track-by="id"
|
track-by="id"
|
||||||
placeholder="Select Category"
|
placeholder="Select Category"
|
||||||
|
:loading="isCategoryLoading"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="">
|
<div class="">
|
||||||
|
|
@ -160,15 +162,25 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, reactive } from "vue";
|
import { ref, reactive, onMounted, onBeforeMount } from "vue";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { createOverlayStore } from "@/stores/OverlayStore";
|
import { createOverlayStore } from "@/stores/OverlayStore";
|
||||||
import Multiselect from "vue-multiselect";
|
import Multiselect from "vue-multiselect";
|
||||||
|
import { departmentAPI, categoryAPI } from "@/core/endpoints/dartachalani";
|
||||||
|
import { createGenericStore } from "@/stores/GenericStore";
|
||||||
|
|
||||||
const ChalaniTowserStore = createOverlayStore("chalani-towser")();
|
const ChalaniTowserStore = createOverlayStore("chalani-towser")();
|
||||||
const { show } = storeToRefs(ChalaniTowserStore);
|
const { show } = storeToRefs(ChalaniTowserStore);
|
||||||
|
|
||||||
|
const departmentStore = createGenericStore("departments")();
|
||||||
|
const { itemList: departments } = storeToRefs(departmentStore);
|
||||||
|
|
||||||
|
const categoryStore = createGenericStore("document-category")();
|
||||||
|
const {itemList: categories } = storeToRefs(categoryStore);
|
||||||
|
|
||||||
const fileInputRef = ref<HTMLInputElement | null>(null);
|
const fileInputRef = ref<HTMLInputElement | null>(null);
|
||||||
|
const isDepartmentLoading = ref(false);
|
||||||
|
const isCategoryLoading = ref(false);
|
||||||
|
|
||||||
const towser = ref({
|
const towser = ref({
|
||||||
title: [{ name: "Chalani", link: "#" }, { name: "Create" }],
|
title: [{ name: "Chalani", link: "#" }, { name: "Create" }],
|
||||||
|
|
@ -198,20 +210,19 @@ const formData = reactive({
|
||||||
attachments: [] as File[],
|
attachments: [] as File[],
|
||||||
});
|
});
|
||||||
|
|
||||||
const departments = ref([
|
|
||||||
{ id: 1, name: "Administration" },
|
|
||||||
{ id: 2, name: "HR" },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const documentCategories = ref([
|
|
||||||
{ id: 1, name: "Confidential" },
|
|
||||||
{ id: 2, name: "Public" },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
|
formData.department = null
|
||||||
|
formData.documentCategory = null
|
||||||
formData.documentNumber = "";
|
formData.documentNumber = "";
|
||||||
formData.subject = "";
|
formData.subject = "";
|
||||||
|
formData.body = "";
|
||||||
|
formData.recipientName = "";
|
||||||
formData.attachments = [];
|
formData.attachments = [];
|
||||||
|
formData.recipientContact = "";
|
||||||
|
formData.recipientAddress = "";
|
||||||
|
formData.issuedDate = "";
|
||||||
|
formData.dispatchDate = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
const onClose = () => {
|
const onClose = () => {
|
||||||
|
|
@ -242,4 +253,16 @@ const handleFileUpload = (event: Event) => {
|
||||||
formData.attachments = [...formData.attachments, ...Array.from(target.files)];
|
formData.attachments = [...formData.attachments, ...Array.from(target.files)];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
onMounted(()=>{
|
||||||
|
departmentStore.fetchList(departmentAPI, {}, isDepartmentLoading);
|
||||||
|
categoryStore.fetchList(categoryAPI, {}, isCategoryLoading);
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeMount(()=>{
|
||||||
|
departmentStore.$reset();
|
||||||
|
departmentStore.$dispose();
|
||||||
|
categoryStore.$reset();
|
||||||
|
categoryStore.$dispose();
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
@ -197,7 +197,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, reactive } from "vue";
|
import { ref, reactive, onMounted, onBeforeMount } from "vue";
|
||||||
import { storeToRefs } from "pinia";
|
import { storeToRefs } from "pinia";
|
||||||
import { createGenericStore } from "@/stores/GenericStore";
|
import { createGenericStore } from "@/stores/GenericStore";
|
||||||
import { createOverlayStore } from "@/stores/OverlayStore";
|
import { createOverlayStore } from "@/stores/OverlayStore";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user