-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logout button, and switching navbar elements depending on login…
… state
- Loading branch information
Showing
5 changed files
with
134 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
button.login-btn:not(.hidden) { | ||
display: block; | ||
} | ||
|
||
button.login-btn.hidden { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
/* global bootstrap */ | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
const loginModal = new bootstrap.Modal(document.getElementById('myModal')); | ||
|
||
document.getElementById('loginBtn').addEventListener('click', () => { | ||
loginModal.show(); | ||
}); | ||
}); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,121 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
/* global bootstrap */ | ||
|
||
async function apiIsLoggedIn() { | ||
try { | ||
const response = await fetch('/api/is_logged_in', { | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
const result = await response.json(); | ||
|
||
if (response.ok) { | ||
return result; | ||
} | ||
console.log(`Error: ${result.message}`); | ||
} catch (error) { | ||
console.log('Something went wrong. Please try again.'); | ||
console.error(error); | ||
} | ||
return false; | ||
} | ||
|
||
async function apiLogin(username, password) { | ||
try { | ||
const response = await fetch('/api/login', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ username, password }), | ||
}); | ||
|
||
const result = await response.json(); | ||
|
||
if (response.ok) { | ||
console.log(`Welcome, ${result.username}!`); | ||
} else { | ||
console.log(`Error: ${result.message}`); | ||
} | ||
} catch (error) { | ||
console.log('Something went wrong. Please try again.'); | ||
console.error(error); | ||
} | ||
} | ||
|
||
async function apiLogout() { | ||
let result; | ||
try { | ||
const response = await fetch('/api/logout', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
result = await response.json(); | ||
|
||
if (response.ok) { | ||
return true; | ||
} | ||
console.log(`Error: ${result.message}`); | ||
return false; | ||
} catch (error) { | ||
console.log('Something went wrong. Please try again.'); | ||
console.error(error); | ||
return false; | ||
} | ||
} | ||
|
||
async function updateNavbar() { | ||
const isLoggedIn = await apiIsLoggedIn(); | ||
|
||
if (isLoggedIn) { | ||
document.getElementById('loginBtn').classList.add('hidden'); | ||
document.getElementById('logoutBtn').classList.remove('hidden'); | ||
|
||
document.getElementById('nav-my-recipes').children[0].classList.remove('disabled'); | ||
document.getElementById('nav-my-account').children[0].classList.remove('disabled'); | ||
} else { | ||
const currentPath = window.location.pathname; | ||
if (currentPath !== '/') { | ||
window.location.href = '/'; | ||
} | ||
|
||
document.getElementById('loginBtn').classList.remove('hidden'); | ||
document.getElementById('logoutBtn').classList.add('hidden'); | ||
|
||
document.getElementById('nav-my-recipes').children[0].classList.add('disabled'); | ||
document.getElementById('nav-my-account').children[0].classList.add('disabled'); | ||
} | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', async () => { | ||
await updateNavbar(); | ||
|
||
const loginModal = new bootstrap.Modal(document.getElementById('myModal')); | ||
|
||
document.getElementById('loginBtn').addEventListener('click', () => { | ||
loginModal.show(); | ||
}); | ||
|
||
document.getElementById('logoutBtn').addEventListener('click', async () => { | ||
await apiLogout(); | ||
|
||
await updateNavbar(); | ||
}); | ||
|
||
document.getElementById('loginForm').addEventListener('submit', async (e) => { | ||
e.preventDefault(); // Prevent the default form submission | ||
|
||
const username = document.getElementById('login-username').value; | ||
const password = document.getElementById('login-password').value; | ||
|
||
try { | ||
const response = await fetch('/api/login', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ username, password }), | ||
}); | ||
|
||
const result = await response.json(); | ||
|
||
if (response.ok) { | ||
console.log(`Welcome, ${result.username}!`); | ||
} else { | ||
console.log(`Error: ${result.message}`); | ||
} | ||
} catch (error) { | ||
console.log('Something went wrong. Please try again.'); | ||
console.error(error); | ||
} | ||
await apiLogin(username, password); | ||
|
||
loginModal.hide(); | ||
await updateNavbar(); | ||
}); | ||
}); |