Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
✨ Impliment dashboard authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
nattui committed May 6, 2020
1 parent cfa772a commit b6eb7e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
44 changes: 20 additions & 24 deletions client/src/js/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ const signup = () => {
console.log('email:', email);
console.log('password:', password);

axios.post('http://localhost:3000/users/signup', { name, email, password })
axios.post('https://kbank-backend.now.sh/users/signup', { name, email, password })
.then(response => {
const { accessToken } = response.data;
localStorage.setItem('token', accessToken);
console.log('accessToken:', accessToken);

// console.log('isAuthenticated:', isAuthenticated());
alert('You have successfully made an account! ✔');
window.location.pathname = 'login.html';
})
.catch(error => console.error(error.response.data));
.catch(error => {
console.error(error.response.data);
alert(error.response.data);
});

event.preventDefault();
}
Expand All @@ -33,37 +35,31 @@ const login = () => {
console.log('email:', email);
console.log('password:', password);

axios.post('http://localhost:3000/users/login', { email, password })
axios.post('https://kbank-backend.now.sh/users/login', { email, password })
.then(response => {
const { accessToken } = response.data;
localStorage.setItem('token', accessToken);
console.log('accessToken:', accessToken);
})
.catch(error => console.error(error.response.data));

event.preventDefault();
}


const isAuthenticated = () => {
const token = localStorage.getItem('token');
const config = { headers: { Authorization: 'Bearer ' + token } };
axios.post('http://localhost:3000/users/authenticate', null, config)
.then(response => {
console.log(response.data);
return true;
alert('You are successfully logged in! ✔');
window.location.pathname = 'dashboard.html';
})
.catch(error => {
console.error(error.response.data);
return false;
alert(error.response.data);
});

event.preventDefault();
}


// Logout of application
const logout = () => {
localStorage.removeItem('token');
// Redirect to another page
}

let isDashboardPage = window.location.pathname === '/dashboard.html' || window.location.pathname === '/dashboard';
if (isDashboardPage && !localStorage.getItem('token')) {
alert('You are not authenticated! ❌');
window.location.pathname = 'login.html';
}

module.exports = { login, signup, isAuthenticated, logout };
module.exports = { login, signup, logout };
1 change: 1 addition & 0 deletions client/src/pages/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<link rel="shortcut icon" href="../resources/images/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="../scss/master.scss">
<link rel="stylesheet" href="../scss/dashboard.scss">
<script defer src="../js/auth.js"></script>
</head>

<body>
Expand Down

1 comment on commit b6eb7e4

@vercel
Copy link

@vercel vercel bot commented on b6eb7e4 May 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.