1

I Would like to use Firebase authentication with my Vue.js app considering the following limitations:

  • I can't use any of the supported providers (Facebook, Google, etc.)
  • I can't use emails - the app is for kids, thus, i would like to use a unique nickname they will choose on sign up instead of an email, this nickname has to be in local rtl language (non-english)
  • I want to use firebase cloud functions as my only server-side code

The the desired outcome would be: a) signing up with nickname, password & full name. b) signing in with nickname and password

How can i authenticate my users with firebase authentication?

Idancn
  • 37
  • 6

2 Answers2

1

Signup

 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);

        }

Login

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
    }
    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
  • Thank you for this. there is one additional limitation i forgot to mention, i would like to allow choosing the nickname in the **local language**, meaning in hebrew, which is an rtl language which probably won't pass firebase email validation rules. maybe transforming the local language nickname to english with some by-directional function can work? are you familiar with such recommended function? – Idancn May 29 '17 at 07:23
1

You can do this using custom authentication with Firebase or using the Firebase Admin SDK for node, and the createUser() function.

Your sign in flow for a user would look something like this:

User enters desired credentials in your signup screen.

You append your domain as an email after the username, say you sign up with a username of chris, then behind the scenes you append @yourdomain.com so it becomes chris@yourdomain.com. This does prevent the option of resetting your password, so keep that in mind.

You can see Frank's answer here on the same topic Username authentication instead of email

Chris
  • 7,830
  • 6
  • 38
  • 72
  • Thanks Chris, can you elaborate a bit more? maybe provide some code examples? as mention in my question the nickname is in non-local rtl language (hebrew). which mean i won't be able to easily concatenate it to @domain.com postfix – Idancn May 29 '17 at 09:12
  • Hmm, I don't know if it matters that you use hebrew characters. If it does, you can always just base64 encode your usernames for use behind the scenes. Then in the frontend you display them as hebrew? @Idancn – Chris May 29 '17 at 09:31
  • so just to make sure i follow: once a user is created, in order to sign it in i have to **a)** fetch user uid with getUserByEmail. **b)** create custom token with createCustomToken(uid). **c)** sign in with that custom token on the client with signInWithCustomToken. is that correct? – Idancn May 29 '17 at 10:29
  • 1
    You should be able to just sign them in with the email and password (where the email is your base64-username@yourdomain.com). @Idancn – Chris May 29 '17 at 10:47