Skip to content

Commit

Permalink
modify: create middleware for auth verification
Browse files Browse the repository at this point in the history
  • Loading branch information
jfaduna committed Nov 24, 2023
1 parent 8ef9167 commit 49def2a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ app.set('views', 'views');
app.use(express.urlencoded({ extended: true }))
app.use(session({ secret: 'samplesecret' }))

const requireLogin = (req, res, next) => {
if (!req.session.user_id) {
return res.redirect('/login')
}
next();
}

app.get('/', (req, res) => {
res.send('Welcome to Home page')
})
Expand Down Expand Up @@ -59,13 +66,14 @@ app.post('/logout', (req, res) => {
res.redirect('/login')
})

app.get('/secret', (req, res) => {
if (!req.session.user_id) {
res.redirect('/login')
}
app.get('/secret', requireLogin, (req, res) => {
res.render('secret')
})

app.get('/topsecret', requireLogin, (req, res) => {
res.send('Top Secret')
})

app.listen(3000, () => {
console.log('Serving on port 3000')
})

0 comments on commit 49def2a

Please sign in to comment.