Firebase: SIgn in/up via GitHub using the same button

只愿长相守 提交于 2020-01-16 08:59:28

问题


I want to allow users to sign in/up via GitHub using firebase by clicking on the same button.

I create a new authentication for every user in the server side. With the little piece of code, I'm able to detect if either the user is new or not:

const provider = new firebase.auth.GithubAuthProvider();
    firebase.auth().signInWithPopup(provider).then((result) => {
        if (result.additionalUserInfo.isNewUser) {
             // The user is new
        } else {
             // The user is old
        }

But, when the function signInWithPopup is called, if the user is a new user, a new authentication is automatically created for him. How can I avoid this? And if the user is already authenticate, how can the user sign in from the client side? Where is the link between the authentication done from the back end with the user that wants to sign in the front end?


回答1:


This is not how OAuth works. If you use an authentication provider like GitHub, they handle auth flow for you. The only thing that you are left with on the frontend side is an idToken with your identity, basic profile info, and a signature so you can as a user using this token. There's no distinction between sign up/sign in actions.

As you have noticed, Firebase is an extra layer in this flow, it creates an account for a user who signs in for the first time. But there's no user limit or extra payment so I wouldn't bother too much about these extra accounts. You might consider periodical cleanups if you care about the security here.

If you want to actually check if the user exists you have to use firebase-admin e.g. in a Firebase Function before the signInWithPopup is called. But still, unless you want to prevent users from signing up, you can hook your server logic into functions.auth.user().onCreate trigger.

To answer your last question, when the user is already signed in, you'll get the user object in firebase.auth().onAuthStateChanged when a page is loaded. Login state is stored by Firebase.js so once you have called signInWithPopup, you don't need extra steps.



来源:https://stackoverflow.com/questions/57046952/firebase-sign-in-up-via-github-using-the-same-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!