简体   繁体   中英

Firebase: how to get getAdditionalUserInfo in onCreate Firebase Cloud function

I'm trying to get some additional data from a SAML login provider. I can see this data in the client but I fail to see how to get this in the back end (firebase functions).

I'm using these in the FE

"firebase": "^9.8.2",
"firebase-functions": "^3.14.1",

And this in the BE

"firebase-admin": "^10.2.0",
"firebase-functions": "^3.21.2",

This is the data and how I get it in the client:

async myproviderSignIn() {
  const provider = new SAMLAuthProvider('saml.myprovider');
  const auth = getAuth();

  const userCredential = await signInWithPopup(auth, provider);
  const credential = SAMLAuthProvider.credentialFromResult(userCredential);

  if (!environment.production) {
    console.log('User:', userCredential, credential);
    console.log(
      'getAdditionalUserInfo:',
      getAdditionalUserInfo(userCredential)
    );
  }
}

This is what I'm after and logged by getAdditionalUserInfo in the client:

{
    "isNewUser": false,
    "providerId": "saml.myprovider",
    "profile": {
        "urn:schac:attribute-def:schacPersonalUniqueCode": "urn:schac:personalUniqueCode:nl:local:diy.myproviderconext.nl:studentid:123456",
        "urn:oid:1.3.6.1.4.1.25178.1.2.9": "diy.myproviderconext.nl",
        "urn:oid:1.3.6.1.4.1.25178.1.2.14": "urn:schac:personalUniqueCode:nl:local:diy.myproviderconext.nl:studentid:123456",
        "urn:oid:0.9.2342.19200300.100.1.3": "student1@diy.myproviderconext.nl",
        "urn:oid:1.3.6.1.4.1.5923.1.1.1.1": [
            "student",
            "employee",
            "staff",
            "member"
        ],
        "urn:mace:dir:attribute-def:eduPersonAffiliation": [
            "student",
            "employee",
            "staff",
            "member"
        ],
        "urn:mace:dir:attribute-def:sn": "One",
        "urn:mace:dir:attribute-def:givenName": "Student",
        "urn:oid:2.5.4.42": "Student",
        "urn:mace:dir:attribute-def:mail": "student1@diy.myproviderconext.nl",
        "urn:oid:2.5.4.4": "One",
        "urn:mace:terena.org:attribute-def:schacHomeOrganization": "diy.myproviderconext.nl"
    }
}

Finally this is my BE on user create trigger. It creates a DB record of the user when a new user is created in Firebase auth. I'd wish to map some of the properties shown above here to the user record in the DB.

export const onCreate = functions.auth
  .user()
  .onCreate((user: UserRecord, context: EventContext) => {
    const timestamp = serverTimestamp();

    const dbUser: DbUser = {
      uid: user.uid,
      name: user.displayName || '',
      firstName: user.displayName || '',
      lastName: '',
      email: user.email,
      photoURL: user.photoURL,
      emailVerified: user.emailVerified,
      createdDate: timestamp,
      lastSeen: timestamp,
      providerData: JSON.parse(JSON.stringify(user.providerData)),
      userDump: JSON.parse(JSON.stringify(user)),
      contextDump: JSON.parse(JSON.stringify(context)),
    };

    // Get additional user data from the UserCredential
    // const additionalUserInfo = getAdditionalUserInfo(user); ???

    const result = getFirestore()
      .collection(constants.dbCollections.users)
      .doc(user.uid)
      .set(dbUser);

    return result;
  });

How do I access these additional properties in my cloud function without relying on the client?

the problem you try to solve is something explained in here:

https://firebase.google.com/docs/auth/admin/custom-claims#node.js

some of the data you described below is stored within the auth.user

const dbUser: DbUser = {
  uid: user.uid,
  name: user.displayName || '',
  firstName: user.displayName || '',
  lastName: '',
  email: user.email,
  photoURL: user.photoURL,
  emailVerified: user.emailVerified,
  createdDate: timestamp,<-----------------------------------------------here
  lastSeen: timestamp,<--------------------------------------------------and here
  providerData: JSON.parse(JSON.stringify(user.providerData)),
  userDump: JSON.parse(JSON.stringify(user)),
  contextDump: JSON.parse(JSON.stringify(context)),
};

but the data i marked as "here" needs to be created via "custom user claims" but its under the FireBaseAnalytics tab neither auth. nor firestore if you insist to retrive this data from Firebase.

But for this data :

  providerData: JSON.parse(JSON.stringify(user.providerData)),
  userDump: JSON.parse(JSON.stringify(user)),
  contextDump: JSON.parse(JSON.stringify(context)),

you should use FireStore or FirebaseRealTimeDatabase i repeat if you insist of getting those data from FireBase because of the 1000byte policy of Custom Claims.

i hope i do understand your question well. But im sure if you add those properties with this

// Set admin privilege on the user corresponding to uid.

getAuth()
  .setCustomUserClaims(uid, { admin: true })
  .then(() => {
    // The new custom claims will propagate to the user's ID token the
    // next time a new one is issued.
  });

you can retrive it with.auth token

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM