Skip to content

sessionClaims content not getting added to the decoded token #1135

Open
@patzj

Description

@patzj

Related issues

N/A

[REQUIRED] Version info

node: 16.14.0

firebase-functions: 3.21.2

firebase-tools: 10.7.1

firebase-admin: 10.2.0

[REQUIRED] Test case

https://cloud.google.com/identity-platform/docs/blocking-functions#setting_custom_and_session_claims

Web App

export default function Home() {
  const { data: user } = useUser();
  const auth = getAuth();

  function signIn() {
    const provider = new GoogleAuthProvider();
    signInWithPopup(auth, provider).then(async (value) => {
      const idTokenResult = await value.user.getIdTokenResult(true);
      console.log(idTokenResult);
    });
  }

  if (!user) {
    return <button onClick={signIn}>Sign In</button>;
  }

  return (
    <div>
      <span>{user.displayName}</span>&nbsp;
      <button onClick={() => signOut(auth)}>Sign Out</button>
    </div>
  );
}

Cloud Function

export const authUserBeforeCreate = functions
  .region(region)
  .auth.user()
  .beforeCreate((user, context) => {
  // codes
  });
});

export const authUserBeforeSignIn = functions
  .region(region)
  .auth.user()
  .beforeSignIn(async (_, context) => {
    return {
      displayName: "Raging Tomato",
      sessionClaims: { signInIpAddress: context.ipAddress },
    };
  });
});

[REQUIRED] Steps to reproduce

The project was originally a GCP project and is using Identity Platform but I had to setup Firebase in it for various reasons.

  1. Deploy a beforeSignIn Cloud Function via firebase-tools
  2. In the web app, sign in the user using signInWithPopup from firebase/auth
  3. Print the idTokenResult
export const authUserBeforeCreate = functions
  .region(region)
  .auth.user()
  .beforeCreate((user, context) => {
  // codes
  });
});

export const authUserBeforeSignIn = functions
  .region(region)
  .auth.user()
  .beforeSignIn(async (_, context) => {
    return {
      displayName: "Raging Tomato",
      sessionClaims: { signInIpAddress: context.ipAddress },
    };
  });
});

[REQUIRED] Expected behavior

The sessionsClaims I returned from the beforeSignIn Cloud Function should be included in the decoded idToken

[REQUIRED] Actual behavior

Claims not getting added when I print the result of the codes below:

signInWithPopup(auth, provider).then(async (value) => {
  const idTokenResult = await value.user.getIdTokenResult(true);
  console.log(idTokenResult);
});

Were you able to successfully deploy your functions?

Yes. Also printing debug logs as expected.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions