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

feat(authentication): add Sign-In Anonymously #176

Merged
merged 57 commits into from Sep 26, 2022

Conversation

trancee
Copy link
Contributor

@trancee trancee commented Aug 14, 2022

Pull request checklist

Please check if your PR fulfills the following requirements:

  • The changes have been tested successfully.
  • A changeset has been created (npm run changeset).

Close #35

@trancee
Copy link
Contributor Author

trancee commented Aug 14, 2022

@robingenz What's the idea behind linkWithGoogle or linkWithXXX? I know there is a method called linkWithCredential but it requires a AuthCredential from Firebase which we do not have.

Did you think something like this, similar to the signInWithGoogle method but then use the result for linkWithCredential?

  public async linkWithGoogle(
    options?: SignInOptions,
  ): Promise<SignInResult> {
    const provider = new GoogleAuthProvider();
    this.applySignInOptions(options || {}, provider);
    const auth = getAuth();
    const userCredential = await signInWithPopup(auth, provider);
    const authCredential =
      GoogleAuthProvider.credentialFromResult(userCredential);
    const user = await linkWithCredential(auth.currentUser, authCredential)
    return this.createSignInResult(user, authCredential);
  }

@robingenz
Copy link
Member

@trancee This would be my first attempt:

public async linkWithGoogle(): Promise<SignInResult> {
    const auth = getAuth();
    if (!auth.currentUser) {
        throw new Error(FirebaseAuthenticationWeb.ERROR_NO_USER_SIGNED_IN);
    }
    const provider = new GoogleAuthProvider();
    const userCredential = await linkWithPopup(auth.currentUser, provider)
    const authCredential = GoogleAuthProvider.credentialFromResult(userCredential);
    return this.createSignInResult(userCredential, authCredential);
}

The user is not provided. The operation always affects the current user.
Options are not needed, because the Firebase JS SDK only needs user and auth provider (see here).

@trancee
Copy link
Contributor Author

trancee commented Aug 14, 2022

@robingenz There are 3 ways to linking:

  • linkWithCredential
  • linkWithPopup
  • linkWithRedirect

We are mainly using linkWithPopup and not linkWithRedirect. Or should there be an option to use either of them?
I am using linkWithPhoneNumber when linking the phone number (currently not implemented). And linkWithCredential is being used when linking email link.

By the way: I have added SignInMethod as a way to avoid duplicating the provider names, I hope that's fine.

@robingenz
Copy link
Member

We are mainly using linkWithPopup and not linkWithRedirect. Or should there be an option to use either of them?

No, we currently only support popups.

I am using linkWithPhoneNumber when linking the phone number (currently not implemented).

👍

And linkWithCredential is being used when linking email link.

👍

By the way: I have added SignInMethod as a way to avoid duplicating the provider names, I hope that's fine.

Good idea! But i suggest to call it SignInProvider (better fits to the config option providers) and to not export it (or is this necessary?). You can also remove the comments. They are not very helpful from my pov.

@trancee
Copy link
Contributor Author

trancee commented Aug 15, 2022

Good idea! But i suggest to call it SignInProvider (better fits to the config option providers) and to not export it (or is this necessary?). You can also remove the comments. They are not very helpful from my pov.

I had name it the same way Google already named it in their docs:
https://firebase.google.com/docs/reference/js/auth.md#signinmethod

However, they also seem to mix it between providerId and signInMethod
https://firebase.google.com/docs/reference/js/auth.authcredential#properties

And I just noticed they also have this for providerId:
https://firebase.google.com/docs/reference/js/auth.md#providerid

Should we do the same, or keep it as SignInProvider or better rename to Provider or something?

@robingenz
Copy link
Member

robingenz commented Aug 15, 2022

Oh, looks like there is no wrong or right. 😅 I leave it up to you. We can keep it that way for now if you want.

@trancee
Copy link
Contributor Author

trancee commented Aug 15, 2022

I have added the linkWithProvider method to simplify the usage of different providers. This is just a suggestion, it would remove a lot of code duplication for basically the same thing. This could also be applied to the signInWith... methods. What do you think?

@robingenz
Copy link
Member

I have added the linkWithProvider method to simplify the usage of different providers. This is just a suggestion, it would remove a lot of code duplication for basically the same thing. This could also be applied to the signInWith... methods. What do you think?

To be honest, I don't like that approach for 3 reasons:

  1. I prefer several smaller methods than one big method, even if you have less duplicate code. From my point of view, it is more understandable and therefore better to maintain (especially for new users). But that is of course a matter of taste!
  2. You have many parameters, which are optional, because they may be needed only for a particular provider. This also means that you cannot force certain parameters when choosing a provider.
  3. I want to keep it equal to the signIn methods which I will not remove until Capacitor 6 as it would be a breaking change.

@trancee
Copy link
Contributor Author

trancee commented Aug 15, 2022

To be honest, I don't like that approach for 3 reasons:

  1. I prefer several smaller methods than one big method, even if you have less duplicate code. From my point of view, it is more understandable and therefore better to maintain (especially for new users). But that is of course a matter of taste!
  2. You have many parameters, which are optional, because they may be needed only for a particular provider. This also means that you cannot force certain parameters when choosing a provider.
  3. I want to keep it equal to the signIn methods which I will not remove until Capacitor 6 as it would be a breaking change.

Thanks for sharing your opinion on this. I guess there are always pros/cons and I understand your point. I will keep single methods for each provider.

@trancee
Copy link
Contributor Author

trancee commented Aug 15, 2022

If the definitions are fine with you, I will start with the implementation.

packages/authentication/README.md Outdated Show resolved Hide resolved
packages/authentication/src/definitions.ts Outdated Show resolved Hide resolved
packages/authentication/src/definitions.ts Outdated Show resolved Hide resolved
packages/authentication/src/definitions.ts Outdated Show resolved Hide resolved
packages/authentication/src/definitions.ts Outdated Show resolved Hide resolved
packages/authentication/src/web.ts Outdated Show resolved Hide resolved
@trancee trancee requested a review from robingenz August 15, 2022 14:07
Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

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

LGTM. You can start with the implementation.

packages/authentication/src/web.ts Outdated Show resolved Hide resolved
Copy link
Member

@robingenz robingenz left a comment

Choose a reason for hiding this comment

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

Thank you! I will review again these days. I just had little time.

@robingenz
Copy link
Member

@trancee Sorry this whole process is taking so long because of me. I'm unfortunately sick at the moment and rarely online. I will review as soon as possible.

@trancee
Copy link
Contributor Author

trancee commented Sep 8, 2022

@trancee Sorry this whole process is taking so long because of me. I'm unfortunately sick at the moment and rarely online. I will review as soon as possible.

No worries. Take your time to recover, this can wait.

@robingenz robingenz added this to the v1.1.0 milestone Sep 24, 2022
@robingenz
Copy link
Member

@trancee I made a few changes on your branch. I hope that was ok. Otherwise we can revert it. I want to get the next release out in a timely manner and had a few more comments.
I would like you to review my changes and help me test them.

@robingenz
Copy link
Member

npm i @capacitor-firebase/authentication@1.0.0-dev.3000cf3.1664034972

@trancee
Copy link
Contributor Author

trancee commented Sep 25, 2022

@trancee I made a few changes on your branch. I hope that was ok. Otherwise we can revert it. I want to get the next release out in a timely manner and had a few more comments. I would like you to review my changes and help me test them.

I had to revert one change where there is a special circumstance. When you link an account, there are ways to use a directly linking method like linkWithProvider which then does not require another linkWithCredental in handleSuccessfulLink otherwise it will result in an error. So I have reverted that and tested it and it works in my test cases.

I also had to add the providerID for PASSWORD in order to be able to unlink from that provider again.

By the way, I was not able to test Google Play Games as I am not able to set it up properly on my side.

@robingenz
Copy link
Member

@trancee Looks great! Thank you so much for your work!

@robingenz
Copy link
Member

@trancee Can we remove the SignInMethod enum? Seems like it is not used anywhere.

@trancee
Copy link
Contributor Author

trancee commented Sep 26, 2022

@trancee Can we remove the SignInMethod enum? Seems like it is not used anywhere.

Yes, I will remove this since it is not being used anywhere.

@robingenz
Copy link
Member

@trancee Great! Thank you so much! I will merge this today.

@robingenz robingenz merged commit 39830a1 into capawesome-team:main Sep 26, 2022
@github-actions github-actions bot mentioned this pull request Sep 26, 2022
Paso pushed a commit to ice-cream-club/capacitor-firebase that referenced this pull request Apr 19, 2023
Co-authored-by: Robin Genz <mail@robingenz.dev>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(authentication): add Sign-In Anonymously
2 participants