0

I'm working on stripe extension but when i pass the uid it gives me error "firebase__WEBPACK_IMPORTED_MODULE_3___default.a.auth.onAuthStateChanged is not a function"...So any suggestion how can i fix this error??

Here is my code :

import {loadStripe} from '@stripe/stripe-js';
import firebase from 'firebase';


const firestore = firebase.firestore();

firebase.auth().onAuthStateChanged((user) => {
  if(user) {
console.log(user.uid) ;
}
});

export async function createCheckoutSession(uid){
  firebase.auth.onAuthStateChanged((user) => {
    if (user){
      const checkoutSessionRef =  firestore
        .collection('customers')
        .doc(user.uid)
        .collection('checkout_sessions')
        .add({
          price: 'price id',
          success_url: window.location.origin,
          cancel_url: window.location.origin,
      });
// Wait for the CheckoutSession to get attached by the extension
    checkoutSessionRef.onSnapshot((snap) => {
      const { error, sessionId } = snap.data();
      if (error) {
    // Show an error to your customer and 
    // inspect your Cloud Function logs in the Firebase console.
      alert(`An error occured: ${error.message}`);
    }
    if (sessionId) {
    // We have a session, let's redirect to Checkout
    // Init Stripe
    const stripe =   loadStripe('pk_test_1234');
    stripe.redirectToCheckout({ sessionId });
    }
  });
  }
}
  )}  

Any suggestion How can i fix this error???

neel
  • 29
  • 3

1 Answers1

1

The onAuthStateChanged function from firebase would work only in client side. Here from my understanding, you are trying to check whether the user has logged in or logged out, but have invoked the onAuthStateChanged listener in backend itself.

Thus move the logic or onAuthStateChanged logic to front-end code. If you actually want to use a logic to check whether user has logged in or not via backend itself, then you might have to resort to cloud functions and handle it using firebase adming SDK.

Also the loadStripe promise should be handled at the top of the file inorder to avoid recreating the Stripe object on every invocation.

Yedhin
  • 2,931
  • 13
  • 19
  • So do you have any suggestion that how can i do that?? Or how can i get Current user from firebase? – neel Mar 25 '21 at 06:40