fix: use session cookies for user auth

This commit is contained in:
RitvikSardana
2024-08-09 12:46:08 +05:30
parent ee8f2ad878
commit 35d393e15d
5 changed files with 15 additions and 44 deletions
+4 -4
View File
@@ -32,11 +32,11 @@ repos:
- id: black
additional_dependencies: ['click==8.0.4']
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: isort
exclude: ".*setup.py$"
- id: prettier
types_or: [javascript, vue, scss,ts]
ci:
autoupdate_schedule: weekly
-23
View File
@@ -1,23 +0,0 @@
<template>
<div class="p-5">
<div class="flex">
<div>Please login to access Helpdesk</div>
<div class="ml-2">
<Button @click="login">Login</Button>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Login",
methods: {
login() {
window.location.replace("/login")
},
},
}
</script>
<style></style>
+6 -11
View File
@@ -204,20 +204,15 @@ export const router = createRouter({
routes,
});
router.beforeEach(async (to,_,next) => {
router.beforeEach(async (to, _, next) => {
const authStore = useAuthStore();
const usersStore = useUserStore();
if (authStore.isLoggedIn){
if (authStore.isLoggedIn) {
await authStore.init();
await usersStore.init();
}
if (to.name === 'Login' && authStore.isLoggedIn) {
next({ name: AGENT_PORTAL_LANDING })
} else if (to.name !== 'Login' && !authStore.isLoggedIn) {
window.location.href = REDIRECT_PAGE
if (!authStore.isLoggedIn) {
window.location.href = REDIRECT_PAGE;
} else {
next()
next();
}
})
});
+3 -4
View File
@@ -1,7 +1,7 @@
import { computed, ComputedRef, Ref, ref } from "vue";
import { defineStore } from "pinia";
import { createResource, call } from "frappe-ui";
import { router, REDIRECT_PAGE} from "@/router";
import { router, REDIRECT_PAGE } from "@/router";
const URI_LOGIN = "login";
const URI_LOGOUT = "logout";
@@ -38,7 +38,7 @@ export const useAuthStore = defineStore("auth", () => {
const timezone: ComputedRef<string> = computed(() => user__.value.time_zone);
function sessionUser() {
let cookies = new URLSearchParams(document.cookie.split("; ").join("&"));
const cookies = new URLSearchParams(document.cookie.split("; ").join("&"));
let _sessionUser = cookies.get("user_id");
if (_sessionUser === "Guest") {
_sessionUser = null;
@@ -59,11 +59,10 @@ export const useAuthStore = defineStore("auth", () => {
},
});
function logout() {
user.value = null;
call(URI_LOGOUT).then(() => {
window.location.href = REDIRECT_PAGE
window.location.href = REDIRECT_PAGE;
});
}
+2 -2
View File
@@ -2,7 +2,7 @@ import { defineStore } from "pinia";
import { createResource } from "frappe-ui";
import { useAuthStore } from "./auth";
import { reactive } from "vue";
import { router } from "@/router";
import { REDIRECT_PAGE } from "@/router";
export const useUserStore = defineStore("user", () => {
const auth = useAuthStore();
@@ -20,7 +20,7 @@ export const useUserStore = defineStore("user", () => {
},
onError(error) {
if (error && error.exc_type === "AuthenticationError") {
router.push("/login");
window.location.href = REDIRECT_PAGE;
}
},
});