Merge branch 'nice' into develop

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-06-20 17:35:01 +07:00
commit fc4404a391
5 changed files with 54 additions and 16 deletions

View file

@ -1,7 +1,9 @@
const ACCESS_TOKEN = "BMAHRISMGT_KEYCLOAK_IDENTITY"; const ACCESS_TOKEN = "BMAHRISMGT_KEYCLOAK_IDENTITY";
const key_C_Config = { const key_C_Config = {
url_Logout: import.meta.env.VITE_URL_SSO, url_Logout: import.meta.env.VITE_URL_SSO,
landing_PageUrl: import.meta.env.VITE_URL_LANDING,
}; };
interface AuthResponse { interface AuthResponse {
access_token: string; access_token: string;
expires_in: number; expires_in: number;
@ -10,22 +12,32 @@ interface AuthResponse {
const authenticated = async () => ((await getToken()) ? true : false); const authenticated = async () => ((await getToken()) ? true : false);
async function setAuthen(r: AuthResponse, val: string) { async function setAuthen(r: AuthResponse, val: string, url?: string) {
await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in); if (r && r.access_token) {
setCookie("SSO", val, r.expires_in); await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in);
window.location.href = "/"; setCookie("SSO", val, r.expires_in);
window.location.href = url ? encodeURI(url) : "/";
}
} }
async function logout() { async function logout(force: boolean = false) {
await deleteCookie(ACCESS_TOKEN); if (!force) {
window.location.href = key_C_Config.url_Logout; await deleteCookie(ACCESS_TOKEN);
window.location.href = key_C_Config.url_Logout;
} else {
const currentUrl = window.location.href;
const loginUrl = `${
key_C_Config.landing_PageUrl
}?system=mgt&redirectUrl=${encodeURIComponent(currentUrl)}`;
window.location.href = loginUrl;
}
} }
async function getToken() { async function getToken() {
return getCookie(ACCESS_TOKEN); return getCookie(ACCESS_TOKEN);
} }
// 2024-08-29T02:55:13.000Z // 2024-08-29T02:55:13.000Z
function setCookie(name: string, value: any, time: number) { async function setCookie(name: string, value: any, time: number) {
let expires = ""; let expires = "";
if (time) { if (time) {
const date = new Date(); const date = new Date();
@ -47,7 +59,7 @@ function getCookie(name: string) {
return null; return null;
} }
function deleteCookie(name: string) { async function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`; document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
} }
@ -71,4 +83,17 @@ async function tokenParsed() {
return JSON.parse(jsonPayload); return JSON.parse(jsonPayload);
} }
export { getToken, authenticated, logout, setAuthen, tokenParsed, getCookie }; async function redirectToLandingPage() {
await deleteCookie(ACCESS_TOKEN);
window.location.href = key_C_Config.landing_PageUrl;
}
export {
getToken,
authenticated,
logout,
setAuthen,
tokenParsed,
getCookie,
redirectToLandingPage,
};

View file

@ -29,6 +29,8 @@ http.interceptors.response.use(
if (typeof error !== undefined) { if (typeof error !== undefined) {
// eslint-disable-next-line no-prototype-builtins // eslint-disable-next-line no-prototype-builtins
if (error.hasOwnProperty("response")) { if (error.hasOwnProperty("response")) {
console.log("error.response", error.response);
if (error.response.status === 403) { if (error.response.status === 403) {
window.location.href = "/error"; window.location.href = "/error";
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message); // Store.commit("SET_ERROR_MESSAGE", error.response.data.message);

View file

@ -123,10 +123,8 @@ const router = createRouter({
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
if (to.meta.Auth) { if (to.meta.Auth) {
const checkAuthen = await authenticated(); const checkAuthen = await authenticated();
console.log("checkAuthen", checkAuthen);
if (!checkAuthen && to.meta.Auth) { if (!checkAuthen && to.meta.Auth) {
logout(); logout(true);
} }
} }
next(); next();

View file

@ -6,7 +6,12 @@ import { storeToRefs } from "pinia";
import { scroll, useQuasar } from "quasar"; import { scroll, useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin"; import { useCounterMixin } from "@/stores/mixin";
import { useMenuDataStore } from "@/stores/menuList"; import { useMenuDataStore } from "@/stores/menuList";
import { tokenParsed, logout, getCookie } from "@/plugins/auth"; import {
tokenParsed,
logout,
getCookie,
redirectToLandingPage,
} from "@/plugins/auth";
import avatar from "@/assets/avatar_user.jpg"; import avatar from "@/assets/avatar_user.jpg";
import http from "@/plugins/http"; import http from "@/plugins/http";
@ -758,7 +763,11 @@ function onViewDetailNoti(url: string) {
เลอกโหมด เลอกโหมด
</div> --> </div> -->
<q-list dense> <q-list dense>
<q-item clickable :href="landingPageUrl" v-if="isSsoToken"> <q-item
clickable
@click="redirectToLandingPage"
v-if="isSsoToken"
>
<q-item-section avatar> <q-item-section avatar>
<q-avatar <q-avatar
color="blue" color="blue"

View file

@ -12,7 +12,11 @@ onMounted(async () => {
expires_in: route.query.expires ? route.query.expires : 36000, expires_in: route.query.expires ? route.query.expires : 36000,
refresh_token: route.query.accessToken, refresh_token: route.query.accessToken,
}; };
setAuthen(params, "y"); setAuthen(
params,
"y",
route.query.redirectUrl ? (route.query.redirectUrl as string) : ""
);
} }
}); });
</script> </script>