From da0022abf0f880c7b52d2cd937ef9d1298fc09cc Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Tue, 5 Nov 2024 15:49:51 -0500 Subject: [PATCH] feat: add puter.auth.whoami() --- src/backend/src/routers/whoami.js | 11 ++++++++++- src/puter-js/src/modules/Auth.js | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/backend/src/routers/whoami.js b/src/backend/src/routers/whoami.js index ea8b9a23f..3660c0184 100644 --- a/src/backend/src/routers/whoami.js +++ b/src/backend/src/routers/whoami.js @@ -24,7 +24,7 @@ const fs = require('../middleware/fs.js'); const _path = require('path'); const eggspress = require('../api/eggspress'); const { Context } = require('../util/context'); -const { UserActorType } = require('../services/auth/Actor'); +const { UserActorType, AppUnderUserActorType } = require('../services/auth/Actor'); // -----------------------------------------------------------------------// // GET /whoami @@ -82,6 +82,15 @@ const WHOAMI_GET = eggspress('/whoami', { delete details.token; } + if ( actor.type instanceof AppUnderUserActorType ) { + details.app_name = actor.type.app.name; + + // IDEA: maybe we do this in the future + // details.app = { + // name: actor.type.app.name, + // }; + } + res.send(details); }) diff --git a/src/puter-js/src/modules/Auth.js b/src/puter-js/src/modules/Auth.js index 5963feaf9..0e4efc928 100644 --- a/src/puter-js/src/modules/Auth.js +++ b/src/puter-js/src/modules/Auth.js @@ -109,6 +109,15 @@ class Auth{ signOut = () =>{ puter.resetAuthToken(); } + + async whoami () { + const resp = await fetch(this.APIOrigin + '/whoami', { + headers: { + Authorization: `Bearer ${this.authToken}` + } + }); + return await resp.json(); + } } export default Auth \ No newline at end of file