Redirect back to previous page when visiting login and sign up pages after user is already logged in

This commit is contained in:
Sahat Yalkabov
2013-12-04 07:44:07 -05:00
parent 3c5f19a6d9
commit 83dbe9c197
5 changed files with 56 additions and 44 deletions

View File

@ -160,7 +160,9 @@ passport.use(new GoogleStrategy({
// Simple route middleware to ensure user is authenticated. Otherwise send to login page. // Simple route middleware to ensure user is authenticated. Otherwise send to login page.
exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) { exports.ensureAuthenticated = function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); } if (req.isAuthenticated()) {
return next();
}
res.redirect('/login'); res.redirect('/login');
}; };

View File

@ -17,6 +17,10 @@ exports.account = function(req, res) {
* GET /login * GET /login
*/ */
exports.getLogin = function(req, res) { exports.getLogin = function(req, res) {
if (req.user) {
return res.redirect('back');
}
res.render('login', { res.render('login', {
title: 'Login', title: 'Login',
user: req.user, user: req.user,
@ -48,6 +52,10 @@ exports.postLogin = function(req, res, next) {
* GET /signup * GET /signup
*/ */
exports.getSignup = function(req, res) { exports.getSignup = function(req, res) {
if (req.user) {
return res.redirect('back');
}
res.render('signup', { res.render('signup', {
title: 'Create Account', title: 'Create Account',
user: req.user, user: req.user,

View File

@ -6,3 +6,4 @@ body {
padding-top: 60px; padding-top: 60px;
} }

View File

@ -176,12 +176,12 @@ a {
} }
.github { .github {
background-color: #000; background-color: #666;
border-color: #000; border-color: #666;
&:focus, &:focus,
&:hover { &:hover {
background-color: transparent; background-color: transparent;
color: #000; color: #666;
} }
} }

View File

@ -1,45 +1,46 @@
extends layout extends layout
block content block content
.login-container .login-wrapper
h3.text-center .login-container
span.text-primary PROJECT h3.text-center
span.text-muted NAME span.text-primary PROJECT
form(method='POST') span.text-muted NAME
.form-group form(method='POST')
input.form-control(type='text', name='usernameOrEmail', id='usernameOrEmail', placeholder='Username or Email', autofocus='') .form-group
.form-group input.form-control(type='text', name='usernameOrEmail', id='usernameOrEmail', placeholder='Username or Email', autofocus='')
input.form-control(type='password', name='password', id='password', placeholder='Password') .form-group
button(type='submit') input.form-control(type='password', name='password', id='password', placeholder='Password')
i.fa.fa-chevron-right button(type='submit')
.form-options.clearfix i.fa.fa-chevron-right
a.pull-right(href='#') Forgot password? .form-options.clearfix
.text-left a.pull-right(href='#') Forgot password?
label.checkbox .text-left
input(type='checkbox') label.checkbox
span Remember me input(type='checkbox')
.row span Remember me
.col-xs-6.col-sm-6.col-md-6 .row
a.btn.btn-primary.btn-block.facebook(href='/auth/facebook') .col-xs-6.col-sm-6.col-md-6
i.fa.fa-facebook a.btn.btn-primary.btn-block.facebook(href='/auth/facebook')
| Facebook i.fa.fa-facebook
.col-xs-6.col-sm-6.col-md-6 | Facebook
a.btn.btn-danger.btn-block.google(href='/auth/google') .col-xs-6.col-sm-6.col-md-6
i.fa.fa-google-plus a.btn.btn-danger.btn-block.google(href='/auth/google')
|   Google i.fa.fa-google-plus
.row |   Google
.col-xs-6.col-sm-6.col-md-6 .row
a.btn.btn-info.btn-block.twitter(href='/auth/twitter') .col-xs-6.col-sm-6.col-md-6
i.fa.fa-twitter a.btn.btn-info.btn-block.twitter(href='/auth/twitter')
|   Twitter i.fa.fa-twitter
.col-xs-6.col-sm-6.col-md-6 |   Twitter
a.btn.btn-default.btn-block.github(href='/auth/github') .col-xs-6.col-sm-6.col-md-6
i.fa.fa-github a.btn.btn-default.btn-block.github(href='/auth/github')
|   GitHub i.fa.fa-github
p Don't have an account yet?  |   GitHub
a(href='signup1.html') Sign up now p Don't have an account yet? 
a(href='/signup') Sign up now
if messages.length if messages.length
.alert.alert-danger .alert.alert-danger
strong= messages strong= messages