-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
authentication.js
77 lines (65 loc) 路 1.46 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
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const orm = require('ormnomnom');
const joi = require('@hapi/joi');
module.exports = class Authentication {
#user = null;
constructor({
id,
provider,
remote_identity,
access_token_enc,
user,
user_id,
created,
modified,
active
}) {
this.id = id;
this.remote_identity = remote_identity;
this.provider = provider;
this.access_token_enc = access_token_enc;
this.#user = user ? Promise.resolve(user) : null;
this.user_id = user_id;
this.created = created;
this.modified = modified;
this.active = active;
}
async serialize() {
const user = await this.user;
const { created, modified, active, provider, remote_identity } = this;
return {
user,
provider,
remote_identity,
created,
modified,
active
};
}
get user() {
if (this.#user === null) {
this.#user = User.objects.get({ id: this.user_id });
this.#user.catch(() => {});
}
return this.#user;
}
set user(u) {
this.#user = Promise.resolve(u);
this.user_id = this.#user.id;
}
};
const User = require('./user');
module.exports.objects = orm(module.exports, {
id: joi
.number()
.integer()
.greater(-1)
.required(),
user: orm.fk(User),
remote_identity: joi.string(),
provider: joi.any().allow(['github']),
access_token_enc: joi.string(),
created: joi.date(),
modified: joi.date(),
active: joi.boolean().default(true)
});