frontend_adaptive_learning/src/roles/guest/auth/services/authService.jsx

77 lines
2.0 KiB
React
Raw Normal View History

2024-10-31 02:32:14 +00:00
import axios from 'axios';
import { API_URL } from '../../../../utils/Constant';
// const API_URL = 'https://8x7r3mdp-3001.asse.devtunnels.ms';
// const login = async (EMAIL, PASSWORD) => {
// try {
2024-11-19 16:02:04 +00:00
// const response = await axios.post(`${API_URL}/login`, { EMAIL, PASSWORD });
2024-10-31 02:32:14 +00:00
// return response.data.payload;
// } catch (error) {
// throw new Error(error.response?.data?.message || 'Login failed');
// }
// };
const login = async (EMAIL, PASSWORD) => {
try {
2024-11-19 16:02:04 +00:00
const response = await axios.post(`${API_URL}/login`, { EMAIL, PASSWORD }, {
2024-10-31 02:32:14 +00:00
withCredentials: true
});
const { TOKEN, refreshToken } = response.data.payload;
localStorage.setItem('token', TOKEN);
const config = {
headers: {
Authorization: TOKEN
}
};
2024-11-19 16:02:04 +00:00
const data = await axios.get(`${API_URL}/getMe`, config);
2024-10-31 02:32:14 +00:00
const profile = data.data.payload;
localStorage.setItem('userData', JSON.stringify({username: profile.NAME_USERS, picture: profile.PICTURE}));
return response.data.payload;
} catch (error) {
throw error;
}
};
const register = async (endpoint, data) => {
try {
2024-11-19 16:02:04 +00:00
const response = await axios.post(`${API_URL}/register/${endpoint}`, data);
2024-10-31 02:32:14 +00:00
return response.data;
} catch (error) {
throw new Error(error.response?.data?.message || 'Registration failed');
}
};
const forgotPassword = async (email) => {
try {
2024-11-19 16:02:04 +00:00
const response = await axios.post(`${API_URL}/forgot-password`, { email });
2024-10-31 02:32:14 +00:00
return response.data;
} catch (error) {
throw new Error(error.response?.data?.message || 'Password reset failed');
}
};
const logout = () => {
localStorage.removeItem('userData');
localStorage.removeItem('token');
};
2024-11-19 12:21:02 +00:00
const validateEmail = async (TOKEN) => {
2024-11-19 08:41:18 +00:00
try {
2024-11-19 16:02:04 +00:00
const response = await axios.post(`${API_URL}/validateEmail`, { TOKEN });
2024-11-19 08:41:18 +00:00
return response.data;
} catch (error) {
throw new Error(error.response?.data?.message || 'Failed Validate');
}
};
2024-10-31 02:32:14 +00:00
export default {
login,
register,
forgotPassword,
logout,
2024-11-19 08:41:18 +00:00
validateEmail
2024-10-31 02:32:14 +00:00
};