在Firebase中创建临时匿名用户 [英] Creating temporary anonymous users in Firebase

查看:117
本文介绍了在Firebase中创建临时匿名用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试自动生成用户帐户,可以将数据保存到数据中,然后将这些帐户提升为正确的用户名/密码帐户。任何想法,最好的方式来做到这一点?

我不清楚我是否可以切换auth提供程序从匿名密码。

$您可以先创建一个 .html>匿名登录,然后将该身份验证uid键存储到某种会话或cookie(取决于您正在使用的框架)。虽然客户端是匿名的,但您可以将保存的数据链接到匿名的uid。



要在登录后将数据传输给用户,您需要使用Firebase的 onAuth()侦听器。这将会在用户的认证数据改变时通知你。一旦你有了新的认证uid,你可以将你存储的数据链接到新的uid并删除你的匿名会话。

以下是Firebase文档中的修改示例:
$ b $ pre $ var firebaseRef =新的Firebase('https://samplechat.firebaseio-demo.com');
firebaseRef.onAuth(函数(authData){
如果(authData){
//客户端已经登录,所以请检查它是匿名用户还是真实用户

如果(authData.provider ==='anonymous'){
//用户是匿名的
//将authData.uid保存到一些本地会话信息中,以跟踪数据
} else {
//用户登录
//将您从匿名会话存储的任何数据传输到
//这个新的authUser.uid

} else {
//客户端未经身份验证

//这将是创建匿名登录
的好地方firebaseRef.authAnonymously(function(error,authData){
if (错误){
//处理登录失败
} else {
//你现在匿名登录,但是你真的不需要做任何事情
//这里,因为你的onAuth()侦听器已经要捕获这个认证更改
}

});

当用户更改其认证信息时,Firebase不会告诉您以前的用户信息是什么,因此您确实需要在某处存储匿名用户标识。您也可以在不创建匿名登录的情况下完成整个任务,只需要存储会话数据直到用户登录,但是匿名登录提供了更高的一致性。

I'm trying auto-generate user accounts that I can save data with, and later promote those to proper username/password accounts. Any ideas on the best way to do that?

It wasn't clear to me whether or not I could switch auth providers from anonymous to password.

解决方案

You can start by creating an anonymous login with Firebase, and then store that auth "uid" key to some sort of session or cookie (depending on what framework you're working with). While the client is anonymous, you can link your saved data to this anonymous "uid".

To transfer the data over to the user after they login, you'll need to use Firebase's onAuth() listener. This will notify you when the user's auth data changes. Once you have the new authenticated uid, you can link your stored data to that new uid and delete your anonymous session.

Here's the modified sample from Firebase docs:

var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com');
firebaseRef.onAuth(function(authData) {
  if (authData) {
    // Client is logged in, so check if it's anonymous or a real user

    if (authData.provider === 'anonymous') {
      // user is anonymous
      // save authData.uid to some local session info, to keep track of data
    } else {
      // user is logged in
      // transfer any data that you had stored from the anonymous session to 
      // this new authUser.uid

  } else {
    // Client is unauthenticated

    // This would be a good spot to create the anonymous login
    firebaseRef.authAnonymously(function(error, authData) {
      if (error) {
        // handle login failure
      } else {
        // you are now anonymously logged in, but you really don't need to do anything
        // here, because your onAuth() listener is already going to catch this auth change
      }
  }
});

Firebase doesn't tell you what the previous uid was when the user changes their auth info, so you really need to store that anonymous uid somewhere. You could also do the whole thing without ever creating the anonymous login, by just storing session data until the user logs in, but the anonymous login provides more consistency.

这篇关于在Firebase中创建临时匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆