feat: enhance userToken function to handle 401 errors and clear user data, update apix type for public port
This commit is contained in:
parent
cc0a9d8651
commit
62229e026f
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue