Skip to content

Commit

Permalink
Cleaner way for redirecting back to the Referrer
Browse files Browse the repository at this point in the history
The 'back' special case has been moved from res.location() to a
new req.back getter. In the future, this will make it possible
to redirect to a regular 'back' page.
  • Loading branch information
ricordisamoa authored and wesleytodd committed Sep 10, 2024
1 parent f9256ef commit 9a41d58
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ app.post('/login', function (req, res, next) {
req.session.success = 'Authenticated as ' + user.name
+ ' click to <a href="/logout">logout</a>. '
+ ' You may now access <a href="/restricted">/restricted</a>.';
res.redirect('back');
res.redirect(req.back);
});
} else {
req.session.error = 'Authentication failed, please check your '
Expand Down
4 changes: 2 additions & 2 deletions examples/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.get('/', function(req, res){

app.get('/forget', function(req, res){
res.clearCookie('remember');
res.redirect('back');
res.redirect(req.back);
});

app.post('/', function(req, res){
Expand All @@ -43,7 +43,7 @@ app.post('/', function(req, res){
res.cookie('remember', 1, { maxAge: minute })
}

res.redirect('back');
res.redirect(req.back);
});

/* istanbul ignore next */
Expand Down
2 changes: 1 addition & 1 deletion examples/route-separation/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ exports.update = function(req, res){
var user = req.body.user;
req.user.name = user.name;
req.user.email = user.email;
res.redirect('back');
res.redirect(req.back);
};
12 changes: 12 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,18 @@ defineGetter(req, 'xhr', function xhr(){
return val.toLowerCase() === 'xmlhttprequest';
});

/**
* Return the referrer, or "/" if not present.
* Useful as argument to res.redirect().
*
* @return {String}
* @public
*/

defineGetter(req, 'back', function back(){
return this.get('Referrer') || '/';
});

/**
* Helper function for creating a getter on an object.
*
Expand Down
3 changes: 2 additions & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ res.location = function location(url) {

// "back" is an alias for the referrer
if (url === 'back') {
loc = this.req.get('Referrer') || '/';
deprecate('res.location("back"): Use res.location(req.back) instead');
loc = this.req.back;
} else {
loc = String(url);
}
Expand Down
8 changes: 4 additions & 4 deletions test/res.location.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('res', function(){
var app = express()

app.use(function (req, res) {
res.location('back').end()
res.location(req.back).end()
})

request(app)
Expand All @@ -65,7 +65,7 @@ describe('res', function(){
var app = express()

app.use(function (req, res) {
res.location('back').end()
res.location(req.back).end()
})

request(app)
Expand All @@ -79,7 +79,7 @@ describe('res', function(){
var app = express()

app.use(function (req, res) {
res.location('back').end()
res.location(req.back).end()
})

request(app)
Expand All @@ -94,7 +94,7 @@ describe('res', function(){
var app = express()

app.use(function (req, res) {
res.location('back').end()
res.location(req.back).end()
})

request(app)
Expand Down

0 comments on commit 9a41d58

Please sign in to comment.