From 196ae7b759ffd4dad95038aa8899ba8339ea7edb Mon Sep 17 00:00:00 2001 From: Dimas Atmodjo Date: Tue, 19 Nov 2024 19:21:02 +0700 Subject: [PATCH] split API --- src/roles/guest/auth/hooks/useValidate.jsx | 4 ++-- src/roles/guest/auth/services/authService.jsx | 14 +++++++------- src/roles/guest/auth/views/ValidateEmail.jsx | 10 +++++----- src/utils/Constant.jsx | 2 +- src/utils/axiosInstance.jsx | 6 ++++-- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/roles/guest/auth/hooks/useValidate.jsx b/src/roles/guest/auth/hooks/useValidate.jsx index 7dbfbcf..328efdc 100644 --- a/src/roles/guest/auth/hooks/useValidate.jsx +++ b/src/roles/guest/auth/hooks/useValidate.jsx @@ -4,10 +4,10 @@ import authService from '../services/authService'; const useValidate = () => { const [error, setError] = useState(null); - const validateEmail = async (token) => { + const validateEmail = async (TOKEN) => { setError(null); try { - const validating = await authService.validateEmail(token); + const validating = await authService.validateEmail(TOKEN); window.location.href = '/login'; } catch (err) { setError(err.message); diff --git a/src/roles/guest/auth/services/authService.jsx b/src/roles/guest/auth/services/authService.jsx index 3d96c8e..4f0d1ea 100644 --- a/src/roles/guest/auth/services/authService.jsx +++ b/src/roles/guest/auth/services/authService.jsx @@ -5,7 +5,7 @@ import { API_URL } from '../../../../utils/Constant'; // const login = async (EMAIL, PASSWORD) => { // try { -// const response = await axios.post(`${API_URL}/login`, { EMAIL, PASSWORD }); +// const response = await axios.post(`${API_URL}/api/login`, { EMAIL, PASSWORD }); // return response.data.payload; // } catch (error) { // throw new Error(error.response?.data?.message || 'Login failed'); @@ -14,7 +14,7 @@ import { API_URL } from '../../../../utils/Constant'; const login = async (EMAIL, PASSWORD) => { try { - const response = await axios.post(`${API_URL}/login`, { EMAIL, PASSWORD }, { + const response = await axios.post(`${API_URL}/api/login`, { EMAIL, PASSWORD }, { withCredentials: true }); const { TOKEN, refreshToken } = response.data.payload; @@ -25,7 +25,7 @@ const login = async (EMAIL, PASSWORD) => { Authorization: TOKEN } }; - const data = await axios.get(`${API_URL}/getMe`, config); + const data = await axios.get(`${API_URL}/api/getMe`, config); const profile = data.data.payload; localStorage.setItem('userData', JSON.stringify({username: profile.NAME_USERS, picture: profile.PICTURE})); @@ -37,7 +37,7 @@ const login = async (EMAIL, PASSWORD) => { const register = async (endpoint, data) => { try { - const response = await axios.post(`${API_URL}/register/${endpoint}`, data); + const response = await axios.post(`${API_URL}/api/register/${endpoint}`, data); return response.data; } catch (error) { throw new Error(error.response?.data?.message || 'Registration failed'); @@ -46,7 +46,7 @@ const register = async (endpoint, data) => { const forgotPassword = async (email) => { try { - const response = await axios.post(`${API_URL}/forgot-password`, { email }); + const response = await axios.post(`${API_URL}/api/forgot-password`, { email }); return response.data; } catch (error) { throw new Error(error.response?.data?.message || 'Password reset failed'); @@ -58,9 +58,9 @@ const logout = () => { localStorage.removeItem('token'); }; -const validateEmail = async (token) => { +const validateEmail = async (TOKEN) => { try { - const response = await axios.post(`${API_URL}/validateEmail`, { token }); + const response = await axios.post(`${API_URL}/api/validateEmail`, { TOKEN }); return response.data; } catch (error) { throw new Error(error.response?.data?.message || 'Failed Validate'); diff --git a/src/roles/guest/auth/views/ValidateEmail.jsx b/src/roles/guest/auth/views/ValidateEmail.jsx index a048dc5..4db3a62 100644 --- a/src/roles/guest/auth/views/ValidateEmail.jsx +++ b/src/roles/guest/auth/views/ValidateEmail.jsx @@ -12,17 +12,17 @@ const ValidateEmail = () => { return new URLSearchParams(useLocation().search); }; - const token = useQuery().get('token'); + const TOKEN = useQuery().get('token'); useEffect(() => { - if (token) { - validateEmail(token); + if (TOKEN) { + validateEmail(TOKEN); console.log('good'); } else { - console.log('No token found in the URL.'); + console.log('No TOKEN found in the URL.'); setIsTokenAvailable(false); } - }, [token]); + }, [TOKEN]); return ( <> diff --git a/src/utils/Constant.jsx b/src/utils/Constant.jsx index c88db5e..edb1ff0 100644 --- a/src/utils/Constant.jsx +++ b/src/utils/Constant.jsx @@ -1,4 +1,4 @@ -export const API_URL = 'http://54.173.167.62/api'; +export const API_URL = 'http://54.173.167.62'; export const slugify = (text) => { if (!text) { diff --git a/src/utils/axiosInstance.jsx b/src/utils/axiosInstance.jsx index 9141315..8c9aa05 100644 --- a/src/utils/axiosInstance.jsx +++ b/src/utils/axiosInstance.jsx @@ -1,8 +1,10 @@ import axios from 'axios'; import { API_URL } from './Constant'; +const URL_API = `${API_URL}/api` + const axiosInstance = axios.create({ - baseURL: API_URL, + baseURL: URL_API, }); axiosInstance.interceptors.request.use( @@ -20,7 +22,7 @@ axiosInstance.interceptors.request.use( const refreshAccessToken = async () => { try { - const response = await axios.post(`${API_URL}/refreshToken`, {}, { withCredentials: true }); + const response = await axios.post(`${URL_API}/refreshToken`, {}, { withCredentials: true }); const token = response.data.payload.TOKEN; localStorage.setItem('token', token);