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 key_C_Config = {
url_Logout: import.meta.env.VITE_URL_SSO,
landing_PageUrl: import.meta.env.VITE_URL_LANDING,
};
interface AuthResponse {
access_token: string;
expires_in: number;
@ -10,22 +12,32 @@ interface AuthResponse {
const authenticated = async () => ((await getToken()) ? true : false);
async function setAuthen(r: AuthResponse, val: string) {
await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in);
setCookie("SSO", val, r.expires_in);
window.location.href = "/";
async function setAuthen(r: AuthResponse, val: string, url?: string) {
if (r && r.access_token) {
await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in);
setCookie("SSO", val, r.expires_in);
window.location.href = url ? encodeURI(url) : "/";
}
}
async function logout() {
await deleteCookie(ACCESS_TOKEN);
window.location.href = key_C_Config.url_Logout;
async function logout(force: boolean = false) {
if (!force) {
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() {
return getCookie(ACCESS_TOKEN);
}
// 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 = "";
if (time) {
const date = new Date();
@ -47,7 +59,7 @@ function getCookie(name: string) {
return null;
}
function deleteCookie(name: string) {
async function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}
@ -71,4 +83,17 @@ async function tokenParsed() {
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) {
// eslint-disable-next-line no-prototype-builtins
if (error.hasOwnProperty("response")) {
console.log("error.response", error.response);
if (error.response.status === 403) {
window.location.href = "/error";
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);

View file

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

View file

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

View file

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