mirror of
https://github.com/HeyPuter/puter
synced 2024-11-15 06:15:47 +00:00
feat(backend): allow services to provide whoami values
This commit is contained in:
parent
dd1d129693
commit
fccabf1bc0
@ -219,6 +219,9 @@ const install = async ({ services, app }) => {
|
||||
|
||||
const { GetUserService } = require('./services/GetUserService');
|
||||
services.registerService('get-user', GetUserService);
|
||||
|
||||
const { DetailProviderService } = require('./services/DetailProviderService');
|
||||
services.registerService('whoami', DetailProviderService);
|
||||
}
|
||||
|
||||
const install_legacy = async ({ services }) => {
|
||||
|
@ -60,6 +60,11 @@ const WHOAMI_GET = eggspress('/whoami', {
|
||||
...(req.new_token ? { token: req.token } : {})
|
||||
};
|
||||
|
||||
// Get whoami values from other services
|
||||
const svc_whoami = req.services.get('whoami');
|
||||
const provider_details = await svc_whoami.get_details({ user: req.user });
|
||||
Object.assign(details, provider_details);
|
||||
|
||||
if ( ! is_user ) {
|
||||
// When apps call /whoami they should not see these attributes
|
||||
// delete details.username;
|
||||
|
29
packages/backend/src/services/DetailProviderService.js
Normal file
29
packages/backend/src/services/DetailProviderService.js
Normal file
@ -0,0 +1,29 @@
|
||||
const BaseService = require("./BaseService")
|
||||
|
||||
/**
|
||||
* A generic service class for any service that enables registering
|
||||
* detail providers. A detail provider is a function that takes an
|
||||
* input object and uses its values to populate another object.
|
||||
*/
|
||||
class DetailProviderService extends BaseService {
|
||||
_construct () {
|
||||
this.providers_ = [];
|
||||
}
|
||||
|
||||
register_provider (fn) {
|
||||
this.providers_.push(fn);
|
||||
}
|
||||
|
||||
async get_details (context) {
|
||||
const details = {};
|
||||
|
||||
for (const provider of this.providers_) {
|
||||
const out = await provider(context);
|
||||
Object.assign(details, out);
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { DetailProviderService }
|
Loading…
Reference in New Issue
Block a user