Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase Auth - unhandled Errors/Exceptions #2461

Closed
cjreimer opened this issue May 7, 2021 · 3 comments
Closed

Firebase Auth - unhandled Errors/Exceptions #2461

cjreimer opened this issue May 7, 2021 · 3 comments

Comments

@cjreimer
Copy link
Contributor

cjreimer commented May 7, 2021

I'm working to integrate Firebase auth into an application. If Firebase issues an error, I'm having trouble catching it if using the Redwood auth interface. The exceptions all go unhandled. Catching exceptions from async functions can be tricky and there seems to be something special about how Firebase is returning promises that seem to require a promise .catch block. A try { await} catch {} arrangement does not appear to work with Firebase's promises.

Currently, the only solution I've found (with the existing RedwoodJS codebase) is to go directly to the firebase API to catch the error.

For context, I'm getting the client and signup functions from the useAuth() hook:
const { client, signUp } = useAuth()

This works to catch an error:

 client.auth().createUserWithEmailAndPassword(data.email, data.password)
   .catch((error) => {
      console.log('Error: ', error)
   })

This doesn't work to catch an error:

try {
  await client.auth().createUserWithEmailAndPassword(data.email, data.password)
} catch (e) {
  console.log('Error', e)
}

And for the RedoodJS part, this doesn't work to catch an error:

signUp({ email: data.email, password: data.password }).catch((error) => {
  console.log('Error: ', error)
})

and neither does a try { await } catch arrangement.

I played with the firebase.ts code in the Auth package and could get it to work if I get rid of the async function and wrapped it in a new promise. The code I got working is:

    signup: (withAuth: oAuthProvider | PasswordCreds = 'google.com') => {
      return new Promise((resolve, reject) => {
        if (isPasswordCreds(withAuth)) {
          client
            .auth()
            .createUserWithEmailAndPassword(withAuth.email, withAuth.password)
            .then((result) => {
              resolve(result)
            })
            .catch((error) => {
              reject(error)
            })
        } else {
          const provider = getProvider(withAuth)
          client
            .auth()
            .signInWithPopup(provider)
            .then((result) => {
              resolve(result)
            })
            .catch((error) => {
              reject(error)
            })
        }
      })
    },
  1. Am I missing something?
  2. Does anyone have any better suggestions for addressing this rather than returning new Promises?

Thanks!

@peterp
Copy link
Contributor

peterp commented May 12, 2021

@cjreimer This looks like a poor implementation on our part. We should be forwarding all the exceptions and promises to the client.

Would you be willing to open a PR that makes this possible?

@cjreimer
Copy link
Contributor Author

Thanks for taking a look, Peter!

Sure, I'll open a PR.

@cjreimer
Copy link
Contributor Author

Closed by #2503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants