Skip to content

Commit

Permalink
frontend: user
Browse files Browse the repository at this point in the history
service for api call, store for store data
  • Loading branch information
rkshaon committed Jul 23, 2024
1 parent 9120de2 commit 5f1a31e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 4 deletions.
50 changes: 50 additions & 0 deletions frontend/src/services/userAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// src/services/userAPI.js

import axios from "axios";
import { API_BASE_URL } from "@/config";
import { refreshToken } from "@/services/refreshToken";

const apiClient = axios.create({
baseURL: API_BASE_URL,
withCredentials: false,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
});

// Request interceptor to add the token
apiClient.interceptors.request.use((config) => {
const token = localStorage.getItem("accessToken");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
}, (error) => {
return Promise.reject(error);
});

// Response interceptor to handle errors
apiClient.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response.status === 401) {
// Handle unauthorized error (e.g., refresh token or redirect to login)
try {
await refreshToken();
return apiClient(error.config); // Retry the original request
} catch (refreshError) {
localStorage.removeItem("accessToken");
window.location.href = "/login"; // Redirect to login page
}
}
return Promise.reject(error);
}
);

export const getUserProfile = () => {
return apiClient.get("/api/v1/users/profile");
};

export default {};
2 changes: 2 additions & 0 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Vuex from "vuex";
import category from "./modules/category";
import setup from "./modules/setup";
import user from "@/store/modules/user";

export default new Vuex.Store({
modules: {
category,
setup,
user,
},
});

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/store/modules/auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// src/store/modules/auth.js

export const state = () => ({
token: localStorage.getItem("token"),
refreshToken: localStorage.getItem("refresh_token"),
Expand Down Expand Up @@ -59,4 +61,4 @@ export const actions = {

export const getters = {
token: (state) => state.token,
};
};
4 changes: 2 additions & 2 deletions frontend/src/store/modules/category.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// /src/store/modules/category.js
// src/store/modules/category.js

import {
getCategoryForAdmin,
Expand Down Expand Up @@ -90,4 +90,4 @@ export default {
mutations,
actions,
getters,
};
};
2 changes: 1 addition & 1 deletion frontend/src/store/modules/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// /src/store/modules/setup.js
// src/store/modules/setup.js

import {
getSetupForAdmin,
Expand Down
34 changes: 34 additions & 0 deletions frontend/src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// src/store/modules/category.js

import {
getUserProfile,
} from "@/services/userAPI";

const state = {
profile: {},
};

const mutations = {
setUserProfile(state, profile) {
state.profile = profile;
},
};

const actions = {
async fetchUserProfile({ commit }) {
const response = await getUserProfile();
commit("setUserProfile", response.data);
},
};

const getters = {
profile: (state) => state.profile,
};

export default {
namespaced: true,
state,
mutations,
actions,
getters,
};

0 comments on commit 5f1a31e

Please sign in to comment.