-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
service for api call, store for store data
- Loading branch information
Showing
6 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |