diff --git a/helpers/user.ts b/helpers/user.ts index 58525be..7ea06a7 100644 --- a/helpers/user.ts +++ b/helpers/user.ts @@ -1,12 +1,31 @@ +import get from "lodash.get"; import { apix } from "../utils/apix"; import api from "../utils/axios"; +import { userRoleMe } from "../utils/getAccess"; export const userToken = async () => { if (process.env.NEXT_PUBLIC_MODE === "dev") { - const user = localStorage.getItem("user"); + let user = localStorage.getItem("user"); + if (user) { - const w = window as any; - w.user = JSON.parse(user); + try { + await userRoleMe(); + } catch (ex: any) { + const error = get(ex, "response.data.meta.message") || ex.message; + if (error === "Request failed with status code 401") { + await apix({ + port: "public", + method: "delete", + path: "/api/destroy-cookies", + }); + localStorage.removeItem("user"); + user = null; + } + } + if (user) { + const w = window as any; + w.user = JSON.parse(user); + } return true; } } else { diff --git a/utils/apix.ts b/utils/apix.ts index 916843b..b4da9de 100644 --- a/utils/apix.ts +++ b/utils/apix.ts @@ -2,7 +2,7 @@ import get from "lodash.get"; import api from "./axios"; type apixType = { - port: "portal" | "recruitment" | "mpp"; + port: "portal" | "recruitment" | "mpp" | "public"; path: string; method?: "get" | "delete" | "post" | "put"; data?: any; @@ -36,6 +36,8 @@ export const apix = async ({ ? process.env.NEXT_PUBLIC_API_RECRUITMENT : port === "mpp" ? process.env.NEXT_PUBLIC_API_MPP + : port === "public" + ? process.env.NEXT_PUBLIC_BASE_URL : "" }${path}`;