I want to get the custom claims for a user in a react-router app on the loader/action that run on the server. To do that, I am calling the User's method getIdTokenResult(false):
const authIdToken = function_that_obtains_token_from_request();
const firebaseServerApp = initializeServerApp(
// https://github.com/firebase/firebase-js-sdk/issues/8863#issuecomment-2751401913
initializeApp(firebaseConfig),
{
authIdToken,
},
);
const auth = getAuth(firebaseServerApp);
await auth.authStateReady();
const user = auth.currentUser;
const claims = await user.getIdTokenResult(false).claims;
When I do this, I get the following error on the last line:
FirebaseError: Firebase: Operations that alter the current user are not supported in conjunction with FirebaseServerApp (auth/operation-not-supported-in-this-environment).
And this is the stack of the error:
at _errorWithCustomMessage (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/util/assert.ts:100:18)
at _serverAppCurrentUserOperationNotSupportedError (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/util/assert.ts:108:10)
at AuthImpl.signOut (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/auth/auth_impl.ts:455:9)
at _logoutIfInvalidated (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/user/invalidation.ts:36:25)
at UserImpl.getIdToken (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/user/user_impl.ts:103:25)
at getIdTokenResult (file:///webapp/node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@firebase/auth/src/core/user/id_token_result.ts:60:17)
I understand why the error is there, but is there a way to obtain the claims on the server side without manually decoding the token and without passing them decoded with the request? I can't see anything in the API.