mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-20 22:55:12 +00:00
Initial commit 🚀
This commit is contained in:
38
4-natours/after-section-14/public/js/login.js
Normal file
38
4-natours/after-section-14/public/js/login.js
Normal file
@@ -0,0 +1,38 @@
|
||||
/* eslint-disable */
|
||||
import axios from 'axios';
|
||||
import { showAlert } from './alerts';
|
||||
|
||||
export const login = async (email, password) => {
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'POST',
|
||||
url: '/api/v1/users/login',
|
||||
data: {
|
||||
email,
|
||||
password
|
||||
}
|
||||
});
|
||||
|
||||
if (res.data.status === 'success') {
|
||||
showAlert('success', 'Logged in successfully!');
|
||||
window.setTimeout(() => {
|
||||
location.assign('/');
|
||||
}, 1500);
|
||||
}
|
||||
} catch (err) {
|
||||
showAlert('error', err.response.data.message);
|
||||
}
|
||||
};
|
||||
|
||||
export const logout = async () => {
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'GET',
|
||||
url: '/api/v1/users/logout'
|
||||
});
|
||||
if ((res.data.status = 'success')) location.reload(true);
|
||||
} catch (err) {
|
||||
console.log(err.response);
|
||||
showAlert('error', 'Error logging out! Try again.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user