dalenguyen / firebase-wordpress-plugin

A plugin that helps to integrate Firebase to WordPress
https://firebase-wordpress-docs.readthedocs.io
GNU General Public License v2.0
110 stars 31 forks source link

How do I get User ID #195

Closed omarghalib closed 2 years ago

omarghalib commented 2 years ago

Hi, I'm trying to get the user id of logged-in firebase user and save it to a php variable, how can I do that?

dalenguyen commented 2 years ago

Hi @omarghalib , do you know which version of the plugin are you using?

omarghalib commented 2 years ago

Hi @dalenguyen , I'm using version 3.8.0

dalenguyen commented 2 years ago

Hi @omarghalib, if you are using that version. The firebase uid can be retrieved using this.

get_user_meta($user_id, 'firebase_uid', true);
omarghalib commented 2 years ago

Thank you @dalenguyen , but I tried it like this: $user_id_txt = get_user_meta($user_id, 'firebase_uid', true); echo $user_id_txt; echo $user_id; and it's not echoing anything

dalenguyen commented 2 years ago

Hi @omarghalib, can you send a snapshot of how the user looks like in the WordPress dashboard?

Did the user log in via the plugin authentication shortcode?

omarghalib commented 2 years ago

Hi @dalenguyen , I'm showing firebase login UI using: echo do_shortcode("[firebaseui_web redirect='/shop/account' ][/firebaseui_web]"); echo do_shortcode("[firebase_logout redirect='/shop/account' button_text='Sign Out'][/firebase_logout]"); And logging in but then I can't get the uid, I can echo it though, using: echo '<input type="text" name="userID" value="will-be-generated" class="getFirebaseUid">'; But I need to save it to variable

dalenguyen commented 2 years ago

Thank you @dalenguyen , but I tried it like this: $user_id_txt = get_user_meta($user_id, 'firebase_uid', true); echo $user_id_txt; echo $user_id; and it's not echoing anything

Which $user_id that you used for testing? Does it show the number of the user?

Can you send me a picture of the user in the WordPress dashboard after they sign in?

omarghalib commented 2 years ago

Hi @dalenguyen I'm not using wordpress authentication in this, The user id I get with this: echo '<input type="text" name="userID" value="will-be-generated" class="getFirebaseUid">'; is the firebase's user that's logged-in . I need to save that id into a variable

dalenguyen commented 2 years ago

So, in this case, you can try this custom script.

    const waitForFirebase = setInterval(() => {
      if (typeof firebase !== 'undefined' && firebase.apps.length) {
        firebase.auth().onAuthStateChanged(async (user) => {
          // trigger functions after user is logged in
          if (user) {
            // User logged in already or has just logged in.
            console.log(user.uid)
          }
        })
        clearInterval(waitForFirebase)
      }
    }, 1000)
omarghalib commented 2 years ago

Thank you so much @dalenguyen :D It worked

dalenguyen commented 2 years ago

Awesome. I'm glad :D