Skip to content

Commit

Permalink
Redirect Users to attempted URL if page required authentication first
Browse files Browse the repository at this point in the history
  • Loading branch information
dstroot committed Mar 5, 2014
1 parent 270ffbb commit 94e2c32
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/passport.js
Expand Up @@ -188,6 +188,7 @@ exports.isAuthenticated = function (req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
req.session.attemptedURL = req.url; // Save URL so we can redirect to it after authentication
res.set('X-Auth-Required', 'true');
req.flash('errors', { msg: 'You must be logged in to reach that page.' });
res.redirect('/login');
Expand Down
1 change: 0 additions & 1 deletion controllers/api.js
Expand Up @@ -315,7 +315,6 @@ module.exports.controller = function(app) {
token_secret: token.tokenSecret
});
client.posts('danielmoyerdesign.tumblr.com', { type: 'photo' }, function (err, data) {
console.log('data: ' + JSON.stringify(data));
res.render('api/tumblr', {
url: '/apilocked',
blog: data.blog,
Expand Down
56 changes: 50 additions & 6 deletions controllers/user.js
Expand Up @@ -45,7 +45,6 @@ module.exports.controller = function (app) {

app.post('/login', function (req, res, next) {


// Begin a workflow
var workflow = new (require('events').EventEmitter)();

Expand Down Expand Up @@ -162,7 +161,16 @@ module.exports.controller = function (app) {
req.flash('errors', { msg: err.message });
return res.redirect('back');
}
return res.redirect('/api');

// Send user on their merry way
if (req.session.attemptedURL) {
var redirectURL = req.session.attemptedURL;
delete req.session.attemptedURL;
res.redirect(redirectURL);
} else {
res.redirect('/api');
}

});

}
Expand Down Expand Up @@ -849,7 +857,16 @@ module.exports.controller = function (app) {
if (err) {
return next(err);
}
return res.redirect('/api');

// Send user on their merry way
if (req.session.attemptedURL) {
var redirectURL = req.session.attemptedURL;
delete req.session.attemptedURL;
return res.redirect(redirectURL);
} else {
return res.redirect('/api');
}

});
});
} else {
Expand Down Expand Up @@ -917,7 +934,16 @@ module.exports.controller = function (app) {
if (err) {
return next(err);
}
return res.redirect('/api');

// Send user on their merry way
if (req.session.attemptedURL) {
var redirectURL = req.session.attemptedURL;
delete req.session.attemptedURL;
return res.redirect(redirectURL);
} else {
return res.redirect('/api');
}

});
});
} else {
Expand Down Expand Up @@ -985,7 +1011,16 @@ module.exports.controller = function (app) {
if (err) {
return next(err);
}
return res.redirect('/api');

// Send user on their merry way
if (req.session.attemptedURL) {
var redirectURL = req.session.attemptedURL;
delete req.session.attemptedURL;
return res.redirect(redirectURL);
} else {
return res.redirect('/api');
}

});
});
} else {
Expand Down Expand Up @@ -1053,7 +1088,16 @@ module.exports.controller = function (app) {
if (err) {
return next(err);
}
return res.redirect('/api');

// Send user on their merry way
if (req.session.attemptedURL) {
var redirectURL = req.session.attemptedURL;
delete req.session.attemptedURL;
return res.redirect(redirectURL);
} else {
return res.redirect('/api');
}

});
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"private": true,
"version": "0.0.2",
"version": "0.0.2-1",
"name": "Skeleton",
"company": "Skeleton, Inc.",
"subdomain": "skeleton-app",
Expand Down
2 changes: 1 addition & 1 deletion public/css/Skeleton.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/Skeleton.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/js/Skeleton.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 94e2c32

Please sign in to comment.