This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathindex.js
117 lines (99 loc) · 3.34 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Debug from 'debug';
import errors from 'feathers-errors';
import bcrypt from 'bcrypt';
import passport from 'passport';
import { Strategy } from 'passport-local';
import { exposeConnectMiddleware } from '../../middleware';
import { successfulLogin } from '../../middleware';
const debug = Debug('feathers-authentication:local');
const defaults = {
usernameField: 'email',
passwordField: 'password'
};
export class Service {
constructor(options = {}) {
this.options = options;
}
checkCredentials(username, password, done) {
const params = {
internal: true,
query: {
[this.options.usernameField]: username
}
};
// Look up the user
this.app.service(this.options.userEndpoint)
.find(params)
.then(users => {
// Paginated services return the array of results in the data attribute.
let user = users[0] || users.data && users.data[0];
// Handle bad username.
if (!user) {
return done(null, false);
}
return user;
})
.then(user => {
// Check password
bcrypt.compare(password, user[this.options.passwordField], function(error, result) {
// Handle 500 server error.
if (error) {
return done(error);
}
// Successful login.
if (result) {
return done(null, user);
}
// Handle bad password.
return done(null, false);
});
})
.catch(done);
}
// POST /auth/local
create(data, params) {
const options = this.options;
let app = this.app;
// Validate username and password, then generate a JWT and return it
return new Promise(function(resolve, reject){
let middleware = passport.authenticate('local', { session: false }, function(error, user) {
if (error) {
return reject(error);
}
// Login failed.
if (!user) {
return reject(new errors.NotAuthenticated('Invalid login.'));
}
// Login was successful. Generate and send token.
// TODO (EK): Maybe the id field should be configurable
const payload = {
id: user.id !== undefined ? user.id : user._id
};
// Get a new JWT and the associated user from the Auth token service and send it back to the client.
return app.service(options.tokenEndpoint)
.create(payload, { internal: true })
.then(resolve)
.catch(reject);
});
middleware(params.req);
});
}
setup(app) {
// attach the app object to the service context
// so that we can call other services
this.app = app;
}
}
export default function(options){
options = Object.assign({}, defaults, options);
debug('configuring local authentication service with options', options);
return function() {
const app = this;
// Initialize our service with any options it requires
app.use(options.localEndpoint, exposeConnectMiddleware, new Service(options), successfulLogin(options));
// Get our initialize service to that we can bind hooks
const localService = app.service(options.localEndpoint);
// Register our local auth strategy and get it to use the passport callback function
passport.use(new Strategy(options, localService.checkCredentials.bind(localService)));
};
}