Skip to content

Commit

Permalink
done withh login signup on HP
Browse files Browse the repository at this point in the history
  • Loading branch information
makon57 committed Jul 1, 2021
1 parent 77e602c commit 01f11de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
20 changes: 19 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,30 @@ const { logoutUser } = require('../auth');
const db = require('../db/models');
const { csrfProtection, asyncHandler } = require('./utils');

const getUserFromSession = async req => {
if (req.session.auth) {
const id = parseInt(req.session.auth.userId);
let user = await db.User.findByPk(id);
if (user) {
user = user.toJSON();
return {userId: user.id, username: user.username};
} else {
req.session.save(err => {
if (err) return next(err);
delete req.session.auth;
})
}
}
return false;
}

router.get('/', csrfProtection, asyncHandler(async(req, res) => {
const trails = await db.Trail.findAll();
res.render('index', ({ trails }));
const user = await getUserFromSession(req)
res.render('index', ({ trails, user }));
}));


router.get('/trail/:id(\\d+)', csrfProtection, asyncHandler(async(req, res, next) => {
const trailId = parseInt(req.params.id, 10);
const trail = await db.Trail.findByPk(trailId);
Expand Down
10 changes: 8 additions & 2 deletions views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ block content
img(src="1889970.png" class="mixtrail-logo")
div(class="nav-div")
input(type="text" id="search-bar" class="search-bar" placeholder="Find your next trail!")
form(action="/" method="post" id="demo" class="demo")
button(type="submit" class="logout-btn") Logout
if (user)
form(action="/" method="post" id="demo" class="demo")
button(type="submit" class="logout-btn") Logout
else
form(action="/" method="post" id="demo" class="demo")
button(type="submit" class="logout-btn") Login
form(action="/" method="post" id="demo" class="demo")
button(type="submit" class="logout-btn") Sign Up
div
h1 MixTrail
div(class="wrapper")
Expand Down

0 comments on commit 01f11de

Please sign in to comment.