fixing check token

This commit is contained in:
Warunee Tamkoo 2024-07-26 10:19:35 +07:00
parent 8d51e6f623
commit f6b2b26998
5 changed files with 33 additions and 21 deletions

View file

@ -64,7 +64,6 @@ if (kcToken && kcRefreshToken) {
token: kcToken,
refreshToken: kcRefreshToken,
});
keycloak.authenticated = true;
// .then((authenticated) => {
// console.log("authenticated", authenticated);
@ -77,8 +76,6 @@ if (kcToken && kcRefreshToken) {
// .catch((err) => {
// console.error("Keycloak initialization failed:", err);
// });
} else {
keycloak.authenticated = false;
}
app.mount("#app");

View file

@ -1,5 +1,5 @@
import Axios, { type AxiosRequestConfig, type AxiosResponse } from "axios";
import keycloak from "./keycloak";
import keycloak, { kcLogout } from "./keycloak";
const http = Axios.create({
timeout: 1000000000, // เพิ่มค่า timeout
@ -33,7 +33,7 @@ http.interceptors.response.use(
// eslint-disable-next-line no-prototype-builtins
if (error.hasOwnProperty("response")) {
if (error.response.status === 401 || error.response.status === 403) {
window.location.href = "/login";
kcLogout();
// Store.commit("SET_ERROR_MESSAGE", error.response.data.message);
// Store.commit("REMOVE_ACCESS_TOKEN")

View file

@ -9,5 +9,17 @@ const keycloakConfig = {
};
const keycloak = new Keycloak(keycloakConfig);
async function kcLogout() {
await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY");
await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH");
keycloak.logout();
window.location.href = "/login";
}
function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}
export default keycloak;
export { keycloakConfig };
export { keycloakConfig, kcLogout };

View file

@ -78,7 +78,7 @@ const router = createRouter({
// authen with keycloak client
router.beforeEach((to, from, next) => {
if (to.meta.Auth) {
if (keycloak.authenticated === false && to.meta.Auth) {
if (keycloak.authenticated === undefined && to.meta.Auth) {
window.location.href = "/login";
}
} else {

View file

@ -4,7 +4,7 @@ import config from "@/app.config";
import { onMounted, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useQuasar } from "quasar";
import keycloak from "@/plugins/keycloak";
import keycloak, { kcLogout } from "@/plugins/keycloak";
import { useCounterMixin } from "@/stores/mixin";
const route = useRoute();
@ -93,20 +93,13 @@ const doLogout = () => {
$q,
async () => {
// authen with keycloak client
keycloak.logout();
await deleteCookie("BMAHRIS_KEYCLOAK_IDENTITY");
await deleteCookie("BMAHRIS_KEYCLOAK_REFRESH");
window.location.href = "/login";
kcLogout();
},
"ยืนยันการออกจากระบบ",
"ต้องการออกจากระบบใช่หรือไม่"
);
};
function deleteCookie(name: string) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}
const clickDelete = async (id: string, index: number) => {
dialogRemove($q, async () => {
// showLoader();
@ -458,19 +451,29 @@ function onInfo() {
><q-item-label>ขอลาออก</q-item-label></q-item-section
>
</q-item>
<q-item clickable v-close-popup>
<q-item-section avatar style="min-width: 30px">
<q-icon color="orange-9" size="18px" name="mdi-lock-outline" />
<q-icon
color="orange-9"
size="18px"
name="mdi-lock-outline"
/>
</q-item-section>
<q-item-section
><q-item-label>เปลยนรหสผาน</q-item-label></q-item-section
><q-item-label
>เปลยนรหสผาน</q-item-label
></q-item-section
>
</q-item>
<q-item clickable v-close-popup @click="doLogout">
<q-item-section avatar style="min-width: 30px">
<q-icon color="primary" size="18px" name="mdi-logout-variant" />
<q-icon
color="primary"
size="18px"
name="mdi-logout-variant"
/>
</q-item-section>
<q-item-section>
<q-item-label>ออกจากระบบ</q-item-label>