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 [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);

View File

@ -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');

View File

@ -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 (
<>

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) => {
if (!text) {

View File

@ -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);