feat: enhance userToken function to handle 401 errors and clear user data, update apix type for public port

This commit is contained in:
faisolavolut 2025-02-18 10:38:40 +07:00
parent cc0a9d8651
commit 62229e026f
2 changed files with 25 additions and 4 deletions

View File

@ -1,12 +1,31 @@
import get from "lodash.get";
import { apix } from "../utils/apix"; import { apix } from "../utils/apix";
import api from "../utils/axios"; import api from "../utils/axios";
import { userRoleMe } from "../utils/getAccess";
export const userToken = async () => { export const userToken = async () => {
if (process.env.NEXT_PUBLIC_MODE === "dev") { if (process.env.NEXT_PUBLIC_MODE === "dev") {
const user = localStorage.getItem("user"); let user = localStorage.getItem("user");
if (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) { if (user) {
const w = window as any; const w = window as any;
w.user = JSON.parse(user); w.user = JSON.parse(user);
}
return true; return true;
} }
} else { } else {

View File

@ -2,7 +2,7 @@ import get from "lodash.get";
import api from "./axios"; import api from "./axios";
type apixType = { type apixType = {
port: "portal" | "recruitment" | "mpp"; port: "portal" | "recruitment" | "mpp" | "public";
path: string; path: string;
method?: "get" | "delete" | "post" | "put"; method?: "get" | "delete" | "post" | "put";
data?: any; data?: any;
@ -36,6 +36,8 @@ export const apix = async ({
? process.env.NEXT_PUBLIC_API_RECRUITMENT ? process.env.NEXT_PUBLIC_API_RECRUITMENT
: port === "mpp" : port === "mpp"
? process.env.NEXT_PUBLIC_API_MPP ? process.env.NEXT_PUBLIC_API_MPP
: port === "public"
? process.env.NEXT_PUBLIC_BASE_URL
: "" : ""
}${path}`; }${path}`;