Login basically working, able to post to correct location's login handler.

This commit is contained in:
jkaplon 2016-07-12 11:09:48 -04:00
parent abdc4084f6
commit 6433d1a69d
2 changed files with 26 additions and 14 deletions

View File

@ -83,12 +83,13 @@ app.use(passport.session());
// As with any middleware it is quintessential to call next() if the user is authenticated
var isAuthenticated = function (req, res, next) {
var loc = req.params.loc;
if (req.isAuthenticated()) {
return next();
res.redirect(req); // I think this is what I want, but might cause re-dir loop.
} else {
// If user NOT authenticated, redirect to login page, do not call next().
res.redirect('/login');
res.redirect('/' + loc + '/login');
}
}
@ -101,18 +102,6 @@ But, for now, order routes like this:
- static routes, ordered more specific to less specific
- dynamic routes, ordered more specific to less specific
********************************************************************************************************/
app.get('/login', function(req, res){
winston.info('GET /login');
res.render('login');
});
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login' }),
function(req, res) {
winston.info('sucessful login');
res.redirect('/:loc/admin'); // will this work, should it be `req`?
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/'); // possible to go back to appropriate :loc?
@ -182,6 +171,7 @@ app.get('/:loc/admin', isAuthenticated, function(req, res) {
res.status(404).send('Not found');
} else {
// TODO: load admin template
res.status(200).send('admin template here...');
}
});
@ -190,6 +180,28 @@ app.post(':loc/admin', isAuthenticated, function(req, res){
// possible to forward req to POST to '/'?
});
app.get('/:loc/login', function(req, res){
var loc = req.params.loc;
winston.info('GET ' + loc + '/login');
res.render('login', {loc}, function(err, html) {
if(err !== null) {
winston.error(err);
} else {
res.send(html);
}
});
});
app.post('/:loc/login',
passport.authenticate('local', { failureRedirect: '/:loc/login' }),
function(req, res) {
winston.info('sucessful login');
//res.redirect('/:loc/admin'); // no worky, :loc is literal here, not repop by param.
//res.redirect(req); // no worky, get https://tenniscourtsopen.com/[object%20Object]
var loc = req.params.loc;
res.redirect('/' + loc + '/admin'); // no worky, undefined here...how to include w/post?
});
app.get('/:loc/status', function(req, res) {
var loc = req.params.loc;
if (loc !== 'tt') {

View File

@ -4,7 +4,7 @@
<h1 class="text-center">TT Tennis Courts Are...</h1>
</div>
<form action="/login" method="post">
<form action="/{{loc}}/login" method="post">
<div>
<label>Username:</label>
<input type="text" name="username"/><br/>