split API

This commit is contained in:
Dimas Atmodjo 2024-11-19 19:21:02 +07:00
parent 53a7fa6494
commit 196ae7b759
5 changed files with 19 additions and 17 deletions

View File

@ -4,10 +4,10 @@ import authService from '../services/authService';
const useValidate = () => { const useValidate = () => {
const [error, setError] = useState(null); const [error, setError] = useState(null);
const validateEmail = async (token) => { const validateEmail = async (TOKEN) => {
setError(null); setError(null);
try { try {
const validating = await authService.validateEmail(token); const validating = await authService.validateEmail(TOKEN);
window.location.href = '/login'; window.location.href = '/login';
} catch (err) { } catch (err) {
setError(err.message); setError(err.message);

View File

@ -5,7 +5,7 @@ import { API_URL } from '../../../../utils/Constant';
// const login = async (EMAIL, PASSWORD) => { // const login = async (EMAIL, PASSWORD) => {
// try { // 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; // return response.data.payload;
// } catch (error) { // } catch (error) {
// throw new Error(error.response?.data?.message || 'Login failed'); // throw new Error(error.response?.data?.message || 'Login failed');
@ -14,7 +14,7 @@ import { API_URL } from '../../../../utils/Constant';
const login = async (EMAIL, PASSWORD) => { const login = async (EMAIL, PASSWORD) => {
try { try {
const response = await axios.post(`${API_URL}/login`, { EMAIL, PASSWORD }, { const response = await axios.post(`${API_URL}/api/login`, { EMAIL, PASSWORD }, {
withCredentials: true withCredentials: true
}); });
const { TOKEN, refreshToken } = response.data.payload; const { TOKEN, refreshToken } = response.data.payload;
@ -25,7 +25,7 @@ const login = async (EMAIL, PASSWORD) => {
Authorization: TOKEN 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; const profile = data.data.payload;
localStorage.setItem('userData', JSON.stringify({username: profile.NAME_USERS, picture: profile.PICTURE})); 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) => { const register = async (endpoint, data) => {
try { 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; return response.data;
} catch (error) { } catch (error) {
throw new Error(error.response?.data?.message || 'Registration failed'); throw new Error(error.response?.data?.message || 'Registration failed');
@ -46,7 +46,7 @@ const register = async (endpoint, data) => {
const forgotPassword = async (email) => { const forgotPassword = async (email) => {
try { 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; return response.data;
} catch (error) { } catch (error) {
throw new Error(error.response?.data?.message || 'Password reset failed'); throw new Error(error.response?.data?.message || 'Password reset failed');
@ -58,9 +58,9 @@ const logout = () => {
localStorage.removeItem('token'); localStorage.removeItem('token');
}; };
const validateEmail = async (token) => { const validateEmail = async (TOKEN) => {
try { try {
const response = await axios.post(`${API_URL}/validateEmail`, { token }); const response = await axios.post(`${API_URL}/api/validateEmail`, { TOKEN });
return response.data; return response.data;
} catch (error) { } catch (error) {
throw new Error(error.response?.data?.message || 'Failed Validate'); throw new Error(error.response?.data?.message || 'Failed Validate');

View File

@ -12,17 +12,17 @@ const ValidateEmail = () => {
return new URLSearchParams(useLocation().search); return new URLSearchParams(useLocation().search);
}; };
const token = useQuery().get('token'); const TOKEN = useQuery().get('token');
useEffect(() => { useEffect(() => {
if (token) { if (TOKEN) {
validateEmail(token); validateEmail(TOKEN);
console.log('good'); console.log('good');
} else { } else {
console.log('No token found in the URL.'); console.log('No TOKEN found in the URL.');
setIsTokenAvailable(false); setIsTokenAvailable(false);
} }
}, [token]); }, [TOKEN]);
return ( return (
<> <>

View File

@ -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) => { export const slugify = (text) => {
if (!text) { if (!text) {

View File

@ -1,8 +1,10 @@
import axios from 'axios'; import axios from 'axios';
import { API_URL } from './Constant'; import { API_URL } from './Constant';
const URL_API = `${API_URL}/api`
const axiosInstance = axios.create({ const axiosInstance = axios.create({
baseURL: API_URL, baseURL: URL_API,
}); });
axiosInstance.interceptors.request.use( axiosInstance.interceptors.request.use(
@ -20,7 +22,7 @@ axiosInstance.interceptors.request.use(
const refreshAccessToken = async () => { const refreshAccessToken = async () => {
try { 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; const token = response.data.payload.TOKEN;
localStorage.setItem('token', token); localStorage.setItem('token', token);