Merge branch 'NiceDev' into develop

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-06-20 17:35:55 +07:00
commit fdbda02712
5 changed files with 50 additions and 14 deletions

View file

@ -1,6 +1,7 @@
const ACCESS_TOKEN = "BMAHRISUSER_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;
@ -10,22 +11,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=user&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 +58,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 +82,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

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

View file

@ -95,7 +95,8 @@ router.beforeEach(async (to, from, next) => {
if (to.meta.Auth) {
const checkAuthen = await authenticated();
if (!checkAuthen && to.meta.Auth) {
logout();
logout(true);
return;
}
}
next();

View file

@ -7,7 +7,12 @@ import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
import CustomComponent from "@/components/CustomDialog.vue";
import avatar from "@/assets/avatar_user.jpg";
import { tokenParsed, logout, getCookie } from "@/plugins/auth";
import {
tokenParsed,
logout,
getCookie,
redirectToLandingPage,
} from "@/plugins/auth";
import { useDataStore } from "@/stores/data";
@ -502,7 +507,7 @@ function onViewDetailNoti(url: string) {
<q-item
clickable
v-close-popup
:href="landingPageUrl"
@click="redirectToLandingPage"
v-if="isSsoToken"
>
<q-item-section avatar style="min-width: 30px">

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>