forked from feathersjs-ecosystem/authentication-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (59 loc) · 1.82 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var feathers = require('feathers');
var rest = require('feathers-rest');
var hooks = require('feathers-hooks');
var memory = require('feathers-memory');
var bodyParser = require('body-parser');
var GithubStrategy = require('passport-github').Strategy;
var errorHandler = require('feathers-errors/handler');
var auth = require('feathers-authentication');
var oauth2 = require('../lib/index');
// Initialize the application
var app = feathers();
app.set('port', 3030);
app.configure(rest())
.configure(hooks())
// Needed for parsing bodies (login)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
// Configure feathers-authentication
.configure(auth({ secret: 'super secret' }))
.configure(oauth2({
name: 'github',
Strategy: GithubStrategy,
clientID: 'your client id',
clientSecret: 'your client secret',
scope: ['user']
}))
.use('/users', memory())
.use(errorHandler());
function customizeGithubProfile() {
return function(hook) {
console.log('Customizing Github Profile');
// If there is a github field they signed up or
// signed in with github so let's pull the email
if (hook.data.github) {
hook.data.email = hook.data.github.email;
}
return Promise.resolve(hook);
};
}
// Authenticate the user using the default
// email/password strategy and if successful
// return a JWT.
app.service('authentication').hooks({
before: {
create: [
auth.hooks.authenticate('jwt')
]
}
});
// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
app.service('users').hooks({
before: {
create: [customizeGithubProfile()],
update: [customizeGithubProfile()]
}
});
app.listen(app.get('port'));
console.log('Feathers authentication with oauth2 auth started on 127.0.0.1:3030');