fix ==> redirectToLink

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2025-06-20 17:34:47 +07:00
parent 370050eab9
commit 6772b0325b
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,
};