-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice.js
More file actions
60 lines (51 loc) · 1.43 KB
/
service.js
File metadata and controls
60 lines (51 loc) · 1.43 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const { keys, assign } = Object
const assert = require('assert')
const authentication = require('feathers-authentication')
const { authenticate } = authentication.hooks
const local = require('feathers-authentication-local')
const jwt = require('feathers-authentication-jwt')
const oauth2 = require('feathers-authentication-oauth2')
const remotePlugins = { oauth2 }
// authenticate the user using the a JWT or
// email/password strategy and if successful
// return a new JWT access token.
module.exports = function () {
const app = this
const config = app.get('authentication')
assert(config, 'must set `authentication` in config.')
app
.configure(authentication(config))
.configure(jwt())
.configure(local(config.local))
keys(config.remote).forEach(name => {
const provider = config.remote[name]
const plugin = remotePlugins[provider.type]
if (!plugin) return
app.configure(plugin(assign(provider, { name, formatter: remoteFormatter })))
})
app.service('authentication').hooks({
before: {
create: [
authenticate(config.strategies)
]
}
})
}
function remoteFormatter(req, res, next) {
const token = res.data
var template = `
<html>
<head>
<script>
window.token = JSON.parse('${JSON.stringify(token)}')
</script>
</head>
<body></body>
</html>
`
res.format({
'text/html': function () {
res.send(template)
}
})
}