-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
45 lines (36 loc) · 1.08 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict'
// external modules
var _ = require('lodash')
var S = require('string')
module.exports = function (opt) {
var seneca = this
var options = opt || {}
function redirect (args, cb) {
var req = this.fixedargs.req$
var kind = args.kind
var shouldRedirect = false
var ct = (req.headers['content-type'] || '').split(';')[0]
if (options.always) {
shouldRedirect = true
}
else if (options[kind] && options[kind].always) {
shouldRedirect = true
}
else if (!_.isUndefined(req.query.redirect)) {
shouldRedirect = S(req.query.redirect).toBoolean()
}
else if ('application/json' === ct) {
shouldRedirect = false
}
else shouldRedirect = false
var redirect
if (shouldRedirect) {
redirect = {
win: _.isString(req.query.win) ? req.query.win : (options[kind] ? options[kind].win : undefined),
fail: _.isString(req.query.fail) ? req.query.fail : (options[kind] ? options[kind].fail : undefined)
}
}
cb(null, redirect)
}
seneca.add({role: 'auth', cmd: 'redirect'}, redirect)
}