refactor(stores): added in axios
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import {User} from "@/types/users";
|
||||
import axios from "axios";
|
||||
import {create} from "zustand";
|
||||
|
||||
export type SessionState = {
|
||||
@@ -16,9 +17,14 @@ export const useSessionStore = create<SessionState>((set) => {
|
||||
user: null, // User is NOT stored in localStorage
|
||||
token: storedToken || null,
|
||||
|
||||
setSession: (user, token) => {
|
||||
setSession: async (user: any, token) => {
|
||||
if (token) {
|
||||
localStorage.setItem("auth_token", token);
|
||||
const response = await axios.get("/api/auth/getuseraccess", {
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
});
|
||||
const data = response.data; //await response.json();
|
||||
user = {...user, roles: data.data};
|
||||
} else {
|
||||
localStorage.removeItem("auth_token");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {create} from "zustand";
|
||||
import {useSessionStore} from "./sessionStore";
|
||||
import {Modules} from "@/types/modules";
|
||||
import axios from "axios";
|
||||
|
||||
interface SettingState {
|
||||
userRoles: Modules[];
|
||||
@@ -19,15 +20,8 @@ export const useGetUserRoles = create<SettingState>()((set) => ({
|
||||
try {
|
||||
//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
|
||||
const {token} = useSessionStore();
|
||||
const response = await fetch(`/api/auth/getuseraccess`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authentication: `Beaer ${token}`,
|
||||
// You can add other headers here if necessary
|
||||
},
|
||||
});
|
||||
const data: FetchModulesResponse = await response.json();
|
||||
const response = await axios.get("/api/auth/getuseraccess", {headers: {Authorization: `Bearer ${token}`}});
|
||||
const data: FetchModulesResponse = response.data; //await response.json();
|
||||
//console.log(data);
|
||||
set({userRoles: data.data});
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {Modules} from "@/types/modules";
|
||||
import axios from "axios";
|
||||
import {create} from "zustand";
|
||||
|
||||
interface SettingState {
|
||||
@@ -17,14 +18,8 @@ export const useModuleStore = create<SettingState>()((set) => ({
|
||||
fetchModules: async () => {
|
||||
try {
|
||||
//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
|
||||
const response = await fetch(`/api/server/modules`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
// You can add other headers here if necessary
|
||||
},
|
||||
});
|
||||
const data: FetchModulesResponse = await response.json();
|
||||
const response = await axios.get(`/api/server/modules`, {});
|
||||
const data: FetchModulesResponse = response.data; //await response.json();
|
||||
//console.log(data);
|
||||
set({modules: data.data});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user