update auth

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-08-28 13:43:21 +07:00
parent 23afdc4df9
commit ff3fb68219
12 changed files with 222 additions and 308 deletions

71
src/plugins/auth.ts Normal file
View file

@ -0,0 +1,71 @@
const ACCESS_TOKEN = "BMAHRIS_KEYCLOAK_IDENTITY";
interface AuthResponse {
access_token: string;
expires_in: number;
refresh_token: string;
}
const authenticated = async () => ((await getToken()) ? true : false);
async function setAuthen(r: AuthResponse) {
await setCookie(ACCESS_TOKEN, r.access_token, r.expires_in);
window.location.href = "/";
}
async function logout() {
await deleteCookie(ACCESS_TOKEN);
window.location.href = "/login";
}
async function getToken() {
return getCookie(ACCESS_TOKEN);
}
// 2024-08-29T02:55:13.000Z
function setCookie(name: string, value: any, time: number) {
let expires = "";
if (time) {
const date = new Date();
date.setTime(date.getTime() + time * 1000);
// date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name: string) {
const nameEQ = name + "=";
const ca = document.cookie.split(";");
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}
async function tokenParsed() {
const token = await getCookie(ACCESS_TOKEN);
if (!token) {
return null;
}
const base64Url = token.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
window
.atob(base64)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
return JSON.parse(jsonPayload);
}
export { getToken, authenticated, logout, setAuthen, tokenParsed };

View file

@ -2,7 +2,8 @@ import axios from 'axios'
import config from 'process'
// import { dotnetPath } from "../path/axiosPath";
// import { getToken } from "@baloise/vue-keycloak";
import keycloak from '@/plugins/keycloak'
// import keycloak from '@/plugins/keycloak'
import { getToken } from './auth'
const axiosInstance = axios.create({
withCredentials: false,
@ -11,7 +12,7 @@ const axiosInstance = axios.create({
// axiosInstance.defaults.baseURL = dotnetPath;
axiosInstance.interceptors.request.use(
async (config) => {
const token = await keycloak.token
const token = await getToken()
config.headers = {
Authorization: `Bearer ${token}`,
}

View file

@ -1,5 +1,5 @@
import Axios, { type AxiosRequestConfig, type AxiosResponse } from 'axios'
import keycloak from './keycloak'
import { getToken } from './auth'
const http = Axios.create({
timeout: 1000000000, // เพิ่มค่า timeout
@ -10,10 +10,9 @@ const http = Axios.create({
http.interceptors.request.use(
async function (config: AxiosRequestConfig<any>) {
// await keycloak.updateToken(1)
config.headers = config.headers ?? {}
const token = keycloak.token
// const token = localStorage.getItem("access_token")
const token = await getToken()
if (token) config.headers.Authorization = `Bearer ${token}`
return config
},

View file

@ -1,70 +1,70 @@
// authen with keycloak client
import Keycloak from 'keycloak-js'
// // authen with keycloak client
// import Keycloak from 'keycloak-js'
const ACCESS_TOKEN = 'BMAHRIS_KEYCLOAK_IDENTITY'
const REFRESH_TOKEN = 'BMAHRIS_KEYCLOAK_REFRESH'
const keycloakConfig = {
url: import.meta.env.VITE_URL_KEYCLOAK,
realm: import.meta.env.VITE_REALM_KEYCLOAK,
clientId: import.meta.env.VITE_CLIENTID_KEYCLOAK,
clientSecret: import.meta.env.VITE_CLIENTSECRET_KEYCLOAK,
}
// const ACCESS_TOKEN = 'BMAHRIS_KEYCLOAK_IDENTITY'
// const REFRESH_TOKEN = 'BMAHRIS_KEYCLOAK_REFRESH'
// const keycloakConfig = {
// url: import.meta.env.VITE_URL_KEYCLOAK,
// realm: import.meta.env.VITE_REALM_KEYCLOAK,
// clientId: import.meta.env.VITE_CLIENTID_KEYCLOAK,
// clientSecret: import.meta.env.VITE_CLIENTSECRET_KEYCLOAK,
// }
const keycloak = new Keycloak(keycloakConfig)
// const keycloak = new Keycloak(keycloakConfig)
async function kcAuthen(access_token: string, refresh_token: string) {
await setCookie(ACCESS_TOKEN, access_token, 1)
await setCookie(REFRESH_TOKEN, refresh_token, 1)
window.location.href = '/'
}
// async function kcAuthen(access_token: string, refresh_token: string) {
// await setCookie(ACCESS_TOKEN, access_token, 1)
// await setCookie(REFRESH_TOKEN, refresh_token, 1)
// window.location.href = '/'
// }
async function kcLogout() {
await deleteCookie(ACCESS_TOKEN)
await deleteCookie(REFRESH_TOKEN)
if (keycloak.authenticated !== undefined) {
keycloak.logout()
}
window.location.href = '/login'
}
// async function kcLogout() {
// await deleteCookie(ACCESS_TOKEN)
// await deleteCookie(REFRESH_TOKEN)
// if (keycloak.authenticated !== undefined) {
// keycloak.logout()
// }
// window.location.href = '/login'
// }
async function getToken() {
return {
token: getCookie(ACCESS_TOKEN),
refresh_token: getCookie(REFRESH_TOKEN),
}
}
// async function getToken() {
// return {
// token: getCookie(ACCESS_TOKEN),
// refresh_token: getCookie(REFRESH_TOKEN),
// }
// }
function setCookie(name: string, value: any, days: number) {
let expires = ''
if (days) {
const date = new Date()
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
expires = '; expires=' + date.toUTCString()
}
document.cookie = name + '=' + (value || '') + expires + '; path=/'
}
// function setCookie(name: string, value: any, days: number) {
// let expires = ''
// if (days) {
// const date = new Date()
// date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000)
// expires = '; expires=' + date.toUTCString()
// }
// document.cookie = name + '=' + (value || '') + expires + '; path=/'
// }
function getCookie(name: string) {
const nameEQ = name + '='
const ca = document.cookie.split(';')
for (let i = 0; i < ca.length; i++) {
let c = ca[i]
while (c.charAt(0) == ' ') c = c.substring(1, c.length)
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length)
}
return null
}
// function getCookie(name: string) {
// const nameEQ = name + '='
// const ca = document.cookie.split(';')
// for (let i = 0; i < ca.length; i++) {
// let c = ca[i]
// while (c.charAt(0) == ' ') c = c.substring(1, c.length)
// if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length)
// }
// return null
// }
function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
}
// function deleteCookie(name: string) {
// document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
// }
export default keycloak
export {
keycloakConfig,
getToken,
kcAuthen,
kcLogout,
ACCESS_TOKEN,
REFRESH_TOKEN,
}
// export default keycloak
// export {
// keycloakConfig,
// getToken,
// kcAuthen,
// kcLogout,
// ACCESS_TOKEN,
// REFRESH_TOKEN,
// }