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

Bug fix: Fixed Firebase promises so that errors/exceptions can be caught #2503

Merged
merged 3 commits into from
May 14, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions packages/auth/src/authClients/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,58 @@ export const firebase = (client: Firebase): AuthClient => {
const getProvider = (providerId: oAuthProvider) => {
return new client.auth.OAuthProvider(providerId)
}
// Firebase auth functions return a goog.Promise which as of 2021-05-12 does
// not appear to work with try {await} catch blocks as exceptions are not caught.
// This client returns a new standard Promise so that the exceptions can be
// caught and no changes are required in common auth code located in AuthProvider.tsx
Comment on lines +30 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooof, that's a good catch 🙌

const repackagePromise = (
fireBasePromise: Promise<any | string>
): Promise<any | string> => {
return new Promise((resolve, reject) => {
fireBasePromise
.then((result) => resolve(result))
.catch((error) => reject(error))
})
}

return {
type: 'firebase',
client,
restoreAuthState: () => client.auth().getRedirectResult(),
login: async (withAuth: oAuthProvider | PasswordCreds = 'google.com') => {
restoreAuthState: () => repackagePromise(client.auth().getRedirectResult()),
login: (withAuth: oAuthProvider | PasswordCreds = 'google.com') => {
if (isPasswordCreds(withAuth)) {
return client
.auth()
.signInWithEmailAndPassword(withAuth.email, withAuth.password)
return repackagePromise(
client
.auth()
.signInWithEmailAndPassword(withAuth.email, withAuth.password)
)
}

const provider = getProvider(withAuth)
return client.auth().signInWithPopup(provider)
},
logout: () => client.auth().signOut(),
signup: async (withAuth: oAuthProvider | PasswordCreds = 'google.com') => {
logout: () => repackagePromise(client.auth().signOut()),
signup: (withAuth: oAuthProvider | PasswordCreds = 'google.com') => {
if (isPasswordCreds(withAuth)) {
return client
.auth()
.createUserWithEmailAndPassword(withAuth.email, withAuth.password)
return repackagePromise(
client
.auth()
.createUserWithEmailAndPassword(withAuth.email, withAuth.password)
)
}

const provider = getProvider(withAuth)
return client.auth().signInWithPopup(provider)
return repackagePromise(client.auth().signInWithPopup(provider))
},
getToken: () => {
const currentUser = client.auth().currentUser
if (currentUser) {
return repackagePromise(currentUser.getIdToken())
}
return new Promise(() => {
return null
})
},
getToken: async () => client.auth().currentUser?.getIdToken() ?? null,
getUserMetadata: async () => client.auth().currentUser,
}
}