start project

This commit is contained in:
Warunee Tamkoo 2024-12-16 17:12:39 +07:00
commit ca3c00b6a1
42 changed files with 1310 additions and 0 deletions

24
src/plugins/axios.ts Normal file
View file

@ -0,0 +1,24 @@
import axios from 'axios'
// import { dotnetPath } from "../path/axiosPath";
// import { getToken } from "@baloise/vue-keycloak";
import { getToken } from './auth'
const axiosInstance = axios.create({
withCredentials: false,
})
// axiosInstance.defaults.baseURL = dotnetPath;
axiosInstance.interceptors.request.use(
async (config) => {
const token = await getToken()
config.headers = {
Authorization: `Bearer ${token}`,
}
return config
},
(error) => {
Promise.reject(error)
}
)
export default axiosInstance

20
src/plugins/filters.ts Normal file
View file

@ -0,0 +1,20 @@
/**
* GLOABL Filters
* - Helper Functions
*/
const filters = {
/**
* compactNumber Social Media 1,000 1K 1,000,000 1M
* : {{ $filters.compactNumber(value) }}
*
* @param val
* @returns
*/
compactNumber(val: number) {
const formatter = Intl.NumberFormat('en', { notation: 'compact' })
return formatter.format(val)
},
}
export default filters

39
src/plugins/http.ts Normal file
View file

@ -0,0 +1,39 @@
import Axios, { type AxiosRequestConfig, type AxiosResponse } from "axios";
const http = Axios.create({
timeout: 1000000000, // เพิ่มค่า timeout
headers: {
"X-Requested-With": "XMLHttpRequest",
},
});
http.interceptors.request.use(
async function (config: AxiosRequestConfig<any>) {
config.headers = config.headers ?? {};
return config;
},
function (error: any) {
return Promise.reject(error);
}
);
http.interceptors.response.use(
function (response: AxiosResponse<any, any>) {
return response;
},
function (error: any) {
if (typeof error !== undefined) {
// eslint-disable-next-line no-prototype-builtins
if (error.hasOwnProperty("response")) {
if (error.response.status === 401 || error.response.status === 403) {
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
// Store.commit("REMOVE_ACCESS_TOKEN")
}
}
}
return Promise.reject(error);
}
);
export default http;

70
src/plugins/keycloak.ts Normal file
View file

@ -0,0 +1,70 @@
// 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 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 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),
// }
// }
// 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 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,
// }