This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopbackUser.js
48 lines (42 loc) · 1.51 KB
/
loopbackUser.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
const debug = require('debug')('loopback:jwt');
class loopbackUser {
createUser() {
debug('creating new user');
return new Promise((resolve, reject) => {
const newUserData = {
firstName: this.user.name.givenName,
lastName: this.user.name.familyName,
auth0Identifier: this.user.id,
email: this.user.email,
password: this.options.password,
};
if (typeof this.options.beforeCreate === 'function') {
this.options.beforeCreate(newUserData, user);
}
this.options.userModel.create(newUserData)
.then(newUser => {
debug('new user created [%s]', newUser.email);
this.loginUser()
.then(token => {
resolve(token);
}).catch(e => {
debug('error while try to login after creating user',
e);
reject(e);
});
})
.catch(e => {
debug('error creating user', e);
reject(e);
});
});
}
loginUser() {
debug('attempting to login user [%s]', this.user.email);
return this.options.userModel.login({
email: this.user.email,
password: this.options.password,
});
}
};
module.exports = loopbackUser;