/
authentication.js
66 lines (56 loc) · 1.88 KB
/
authentication.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const authentication = require('feathers-authentication');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const oauth2 = require('feathers-authentication-oauth2');
const GoogleStrategy = require('passport-google-oauth20');
const FacebookStrategy = require('passport-facebook');
const GithubStrategy = require('passport-github');
const verifyHooks = require('feathers-authentication-management').hooks;
const errors = require('feathers-errors');
const _ = require('lodash');
module.exports = function () {
const app = this;
const config = app.get('authentication');
// Set up authentication with the secret
app.configure(authentication(config));
app.configure(jwt());
app.configure(local(config.local));
app.configure(oauth2(Object.assign({
name: 'google',
Strategy: GoogleStrategy
}, config.google)));
app.configure(oauth2(Object.assign({
name: 'facebook',
Strategy: FacebookStrategy
}, config.facebook)));
app.configure(oauth2(Object.assign({
name: 'github',
Strategy: GithubStrategy
}, config.github)));
// The `authentication` service is used to create a JWT.
// The before `create` hook registers strategies that can be used
// to create a new valid JWT (e.g. local or oauth2)
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(config.strategies),
// verifyHooks.isVerified()
],
remove: [
authentication.hooks.authenticate('jwt')
]
},
after: {
create: [
hook => {
if(!_.get(hook, 'params.user')) {
return Promise.reject(new errors.Forbidden('Credentials incorrect'));
}
hook.result.user = hook.params.user;
// Don't expose sensitive information.
delete hook.result.user.password;
}
]
}
});
};