Skip to content

Commit fccabf1

Browse files
committed
feat(backend): allow services to provide whoami values
1 parent dd1d129 commit fccabf1

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

packages/backend/src/CoreModule.js

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ const install = async ({ services, app }) => {
219219

220220
const { GetUserService } = require('./services/GetUserService');
221221
services.registerService('get-user', GetUserService);
222+
223+
const { DetailProviderService } = require('./services/DetailProviderService');
224+
services.registerService('whoami', DetailProviderService);
222225
}
223226

224227
const install_legacy = async ({ services }) => {

packages/backend/src/routers/whoami.js

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const WHOAMI_GET = eggspress('/whoami', {
6060
...(req.new_token ? { token: req.token } : {})
6161
};
6262

63+
// Get whoami values from other services
64+
const svc_whoami = req.services.get('whoami');
65+
const provider_details = await svc_whoami.get_details({ user: req.user });
66+
Object.assign(details, provider_details);
67+
6368
if ( ! is_user ) {
6469
// When apps call /whoami they should not see these attributes
6570
// delete details.username;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const BaseService = require("./BaseService")
2+
3+
/**
4+
* A generic service class for any service that enables registering
5+
* detail providers. A detail provider is a function that takes an
6+
* input object and uses its values to populate another object.
7+
*/
8+
class DetailProviderService extends BaseService {
9+
_construct () {
10+
this.providers_ = [];
11+
}
12+
13+
register_provider (fn) {
14+
this.providers_.push(fn);
15+
}
16+
17+
async get_details (context) {
18+
const details = {};
19+
20+
for (const provider of this.providers_) {
21+
const out = await provider(context);
22+
Object.assign(details, out);
23+
}
24+
25+
return details;
26+
}
27+
}
28+
29+
module.exports = { DetailProviderService }

0 commit comments

Comments
 (0)