added login/logout functionality, i think

This commit is contained in:
Dylan K 2021-09-14 11:10:25 -05:00
parent 0e4f54c96c
commit 0a40c0fe49
7 changed files with 68 additions and 1 deletions

View File

View File

View File

View File

View File

@ -0,0 +1,52 @@
async function signupFormHandler(event) {
event.preventDefault();
// Double check these HTML values
const username = document.querySelector(`#username-signup`).nodeValue.trim();
const email = document.querySelector(`#email-signup`).nodeValue.trim();
const password = document.querySelector(`#password-signup`).nodeValue.trim();
if (username && email && password) {
const response = await fetch(`/api/users`, {
method: 'post',
body: JSON.stringify({
username,
email,
password
}),
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
console.log(`account created`);
} else {
alert(response.statusText);
}
}
}
async function loginFormHandler(event) {
event.preventDefault();
// Double check these HTML values
const email = document.querySelector(`#email-login`).value.trim();
const password = document.querySelector(`#password-login`).value.trim();
if (email && password) {
const response = await fetch(`/api/users/login`, {
method: 'post',
body: JSON.stringify({
email,
password
}),
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
// Am I sticking with dashboard for this????
document.location.replace(`/dashboard`);
} else {
alert(response.statusText);
}
}
}

View File

@ -0,0 +1,15 @@
async function logout() {
const response = await fetch(`api/users/logout`, {
method: 'post',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
document.location.replace(`/`);
} else {
alert(response.statusText);
}
}
// Double check that #logout is actually the right name
document.querySelector(`#logout`).addEventListener(`click`, logout);

View File

@ -13,7 +13,7 @@ const hbs = exphbs.create();
const SequelizeStore = require(`connect-session-sequelize`)(session.Store);
const sess = {
secret: 'CHANGE THIS AT SOME POINT to process.env.PORT and, ya know, do that.',
secret: 'CHANGE THIS AT SOME POINT to process.env.DB_SECRET and, ya know, do that.',
cookie: {},
resave: false,
saveUninitialized: true,