Firebase Auth Anonymous Login

前端 未结 3 2423
囚心锁ツ
囚心锁ツ 2020-12-24 14:11

When using Firebase Auth Anonymous Account it occasionally creates a new UserID in the system and sometimes it uses the same UserID. I really want this to create the same Us

相关标签:
3条回答
  • 2020-12-24 14:37

    So, in my case I wanted to show a login screen with signup but also the option to Skip login. If the user skips the login multiple times, I was doing signInAnonymous which I though it was reusing the user, but no.

    So I solved it like this:

    componentDidMount() {
    
        this.unsubscriber = firebase.auth().onAuthStateChanged((user) => {
          this.setState({ user: user, loadingUser: false}, () => {
            if (this.state.user != null && this.state.user.isAnonymous == false)
              this.startApp();
          });
        });
    
      }
    
    
    
    
    skip = () => {
        this.setState({loading: true}, () => {
          if (this.state.user != null && this.state.user.isAnonymous == true)
            this.startApp();
          else {
            firebase.auth().signInAnonymouslyAndRetrieveData().then((result) => {
              this.setState({user: result.user}, () => {
                this.startApp()
              })
            })
            .catch(err => {
              this.setState({loading: false});
              alert(err)
            });
          }
    
        });
    
      }
    

    Code is React Native but it should help you see the logic. I wait for the auth state and store the user temporarly. If it is not anonymous I start the home screen. If it is anonymous I give the user the option to register. If it still wants to skip, then I just start the app so I can re use the ID.

    0 讨论(0)
  • 2020-12-24 14:40

    You can use the Custom Auth System. Since the "Play as Guest" will only be specified for a special device. You can use Device ID as the CustomToken and then call Firebase Authentication Method:

    auth.SignInWithCustomTokenAsync(custom_token).ContinueWith{.......
    

    You can provide "custom_token" as the Device ID. You can get Device ID by:

    string deviceID = SystemInfo.deviceUniqueIdentifier;
    

    Hope this helps...

    0 讨论(0)
  • 2020-12-24 14:46

    When you call SignOut, your guest will signout forever. If you want to keep your guest, don't call SignOut method.

    0 讨论(0)
提交回复
热议问题