【问题标题】:Firebase authentication with custom identifiers使用自定义标识符的 Firebase 身份验证
【发布时间】:2017-10-29 08:39:26
【问题描述】:

考虑到以下限制,我想在我的 Vue.js 应用中使用 Firebase 身份验证:

  • 我无法使用任何受支持的提供商(Facebook、Google 等)
  • 我不能使用电子邮件 - 该应用程序是为孩子们准备的,因此,我想使用他们在注册时选择的唯一昵称而不是电子邮件,这个昵称必须是本地 rtl 语言(非英语)
  • 我想使用 firebase 云函数作为我唯一的服务器端代码

期望的结果是: a) 使用昵称、密码和全名注册。 b) 使用昵称和密码登录

如何使用 firebase 身份验证来验证我的用户?

【问题讨论】:

    标签: firebase firebase-authentication


    【解决方案1】:

    您可以通过 Firebase 使用 custom authentication 或使用 Firebase Admin SDK for node 和 createUser() 函数来执行此操作。

    您的用户登录流程如下所示:

    用户在您的注册屏幕中输入所需的凭据。

    您将域作为电子邮件附加在用户名之后,假设您使用用户名 chris 注册,然后在幕后附加 @yourdomain.com,因此它变为 chris@yourdomain.com。这确实会阻止重置密码的选项,因此请记住这一点。

    你可以在这里看到弗兰克关于同一主题的回答Username authentication instead of email

    【讨论】:

    • 谢谢克里斯,你能详细说明一下吗?也许提供一些代码示例?正如我的问题中提到的,昵称是非本地 rtl 语言(希伯来语)。这意味着我将无法轻松地将其连接到 @domain.com 后缀
    • 嗯,我不知道你使用希伯来字符是否重要。如果是这样,您总是可以只对您的用户名进行 base64 编码,以便在幕后使用。然后在前端将它们显示为希伯来语? @Idancn
    • 所以只是为了确保我遵循:一旦创建用户,为了登录,我必须 a) 使用 getUserByEmail 获取用户 uid。 b) 使用 createCustomToken(uid) 创建自定义令牌。 c) 使用 signInWithCustomToken 在客户端上使用该自定义令牌登录。对吗?
    • 您应该能够使用电子邮件和密码(其中电子邮件是您的 base64-username@yourdomain.com)登录。 @Idancn
    【解决方案2】:

    注册

     NickName=nickNameText.getText().toString.trim()+"@firebase.com";
         Password  = passwordText.getText().toString().trim();
    
            firebaseAuth.createUserWithEmailAndPassword(NickName, Password)
                            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    //checking if success
                                    if(task.isSuccessful()){
                                        //display some message here
                                        if(type=="User") {
    
    
                                            task.getResult().getUser().getUid();
                                            AddUserInfo(task.getResult().getUser().getUid());
    
    
                                        }else
                                        {
                                            AddAlimInfo(task.getResult().getUser().getUid());
                                        }
    
                                    Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                                        intent.putExtra("Type",TypeSpinnerStr);
                                        startActivity(intent);
    
    
                                      //  startActivity(new Intent(Signup_Activity.this, MainActivity.class));
                                        Toast.makeText(getApplicationContext(),"Successfully registered",Toast.LENGTH_LONG).show();
                                    }else{
                                        //display some message here
                                        Toast.makeText(getApplicationContext(),"Registration Error",Toast.LENGTH_LONG).show();
                                    }
                                    progressDialog.dismiss();
                                }
                            });
    
                }
    
    
    
    
        private void AddUserInfo(String id) {
                  UserClass User=new UserClass();
                    User.setUserID(id);
                    User.setUserName(Name);
                   Email+="@firebase.com";
                    User.setUserNickname(NickName);
                    User.setUserPassword(Password);
                 mdatabaseReference.child("Users").child(UserID).setValue(User);
    
            }
    

    登录

    private void userLogin(){
    
    
    
            nickName= editTextNickName.getText().toString().trim();
           password  = editTextPassword.getText().toString().trim();
    
    
    
    
    
                //if the email and password are not empty
                //displaying a progress dialog
    
    
                progressDialog.setMessage("Logging in  Please Wait...");
                progressDialog.show();
    
                //logging in the user
                firebaseAuth.signInWithEmailAndPassword(nickName, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
    
                                //if the task is successfull
                                if(task.isSuccessful()){
                                    //start the profile activity
    
    
                                    initFirebase();
                 //Check in user Node that whether data exists or not
                //if exists then login else show Snakbar data does not exist
    
                                mAuthUserStr = mAuth.getCurrentUser().getUid();
                                     mEmail=mAuth.getCurrentUser().getEmail();
    
                                    AddEventFireBaseListner(mAuthUserStr,TypeSpinnerStr);
    
    
    
    
                                    //  startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                }
                            }
                        });
    
            }//end of user 
    
     public void AddEventFireBaseListner(String uid,String userType) {
    
    
    //     mdatabaseReference.child("Users").child(UserID).setValue(User);
    
    
        circular_progress2.setVisibility(View.VISIBLE);
    
    
    
    
    mdatabaseReference.child("Users").child(uid).orderByKey("user_id").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
    
         //Compare your Auth Email with user Login email
        }
        }
    

    【讨论】:

    • 谢谢你。还有一个我忘了提及的额外限制,我想允许选择 本地语言 中的昵称,意思是希伯来语,这是一种可能无法通过 firebase 电子邮件验证规则的 rtl 语言。也许通过一些间接功能将当地语言昵称转换为英语可以工作?你熟悉这样的推荐功能吗?
    猜你喜欢
    • 2020-04-01
    • 2022-12-10
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2022-09-28
    • 2020-08-24
    相关资源
    最近更新 更多