0

New to vue/firebase. I was able to lookup how to pull up “current user” data from auth no problem but trying to write a js composable that I can pass in any user id (not necessarily current user) and it will return the user object or at least displayName.

All the docs/vids I can find on the topic reference getting info on *current user *only not another user. From the Google Docs it says I should be able to do this in the "Retrieve user data" section. Closest model to Vue code-wise seems to be “Node.Js” but it isn't working.

Here's what I've got in getUserById

import { getAuth } from 'firebase/auth'

const getUserById = (u) => { // u = user id
    const userData = null
    getAuth()
        .getUser(u)
        .then((userRecord) => {
            // See the UserRecord reference doc for the contents of userRecord.
            console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
            userData = userRecord
        })
        .catch((error) => {
            console.log('Error fetching user data:', error);
        });

    return { userData }
}
export default getUserById

The error I get is getUser is not a function. I tried adding getUser to the import but same error.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
BGMX
  • 25
  • 7

1 Answers1

0

There is no way to look up information about just any user by their UID in the client-side APIs of Firebase as that would be a security risk. There is an API to look up a user by their UID in the Admin SDK, but that can only be used on a trusted environment (such as your development machine, a server you control, or Cloud Functions/Cloud Run), and not in client-side code.

If you need such functionality in your app, you can either wrap the functionality from the Admin SDK in a custom endpoint that you then secure, or have each user write information to a cloud database (such as Realtime Database or Firestore) and read it from there.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for the reply. The use case is something along the lines of a user makes a post then other users can contribute. Each time someone contributes, their ID is associated in the DB since name could be changed. I would like to show the names of everyone who contributed. If I understand, you are saying the only way to do that would be to create a lookup table/document outside of Auth? – BGMX Jul 09 '22 at 00:01
  • Yes, that's indeed what I explained in my answer and what the links also show. There is no way to look up information about just any user by their UID in the client-side APIs of Firebase as that would be a security risk, so you'll have to take control of that information yourself. – Frank van Puffelen Jul 09 '22 at 00:17