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