How to specify a username with a username and NOT by email?

I would like to use Firebase for my web application designed for people with dementia in home care. They do not have electronic or social network accounts, so you need a simple username / password to log in / out.

What is the easiest way to do this? From what I see in the docs, I have to use a custom auth stream, but I don't have an existing auth server.

If I really need to do this, is this the easiest way to provide a token? Azure has features and AWS has Lambda, but I don’t see anything here Firebase

+23
source share
4 answers

You are correct that logging in using a username and password is not supported initially in Firebase Auth.

You can implement your own provider, as shown in this example . This allows you to fulfill any user requirements, but admittedly a little more complicated than using built-in providers.

A workaround that you can take without using special authentication with another backend is to accept the usernames in your user interface, but in the basic logic add β€œ @yourowndomain.com ” before calling functions to register or log in by email.

This way you will use email / password authentication by matching <username> with <username>@yourowndomain.com

+13
source

Adding a dummy domain at the end is a kind of patch and should be avoided. To include a username, follow these simple steps.

Sign up

Use the user ID, email address, and password during registration. Register your user using a regular email and password. If successful, save the letter against user_id in a separate node (branches).

 mButtonSignUp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(isValid()){ mProgressBar.setVisibility(View.VISIBLE); final String userId = mEditTextUserId.getText().toString(); final String emailId = mEditTextEmail.getText().toString() ; String password = mEditTextPassword.getText().toString() ; firebaseRef.createUser(emailId, password, new Firebase.ResultHandler() { @Override public void onSuccess() { firebaseRef.child("user_ids").child(userId).setValue(emailId); Toast.makeText(getBaseContext(),"You are successfully registered ",Toast.LENGTH_SHORT).show(); mProgressBar.setVisibility(View.GONE); } @Override public void onError(FirebaseError firebaseError) { mProgressBar.setVisibility(View.GONE); Toast.makeText(getBaseContext(),firebaseError.toString(),Toast.LENGTH_SHORT).show(); } }); } } }); 

Database

The database structure will look like this

enter image description here

entrance

Check if the user has entered an email address or user ID. If this is an email id, then log in directly with it, otherwise get the email id associated with the username and sign in.

  Button buttonLogIn = (Button)findViewById(R.id.button_login); buttonLogIn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mProgressBar.setVisibility(View.VISIBLE); String username = mEditTextEmail.getText().toString() ; final String password = mEditTextPassWord.getText().toString() ; // Check if it is an email or not if(android.util.Patterns.EMAIL_ADDRESS.matcher(username).matches()) { performLogin(username,password); }else{ //get the emailId associated with the username firebaseRef.child("user_ids").child(username) .addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot!=null){ String userEmail = dataSnapshot.getValue(String.class); performLogin(userEmail,password); } } @Override public void onCancelled(FirebaseError firebaseError) { //Handle Error } }); } } }); 

 private void performLogin(String emailId, String password) { firebaseRef.authWithPassword(emailId,password, new Firebase.AuthResultHandler() { @Override public void onAuthenticated(AuthData authData) { uid = authData.getUid() ; Toast.makeText(getBaseContext(), authData.toString(), Toast.LENGTH_SHORT).show(); } @Override public void onAuthenticationError(FirebaseError firebaseError) { Toast.makeText(getBaseContext(), firebaseError.toString(), Toast.LENGTH_SHORT).show(); } }); } 
+10
source

You can use sign in with custom token

Firebase gives you full control over authentication, allowing you to authenticate users or devices using secure JSON Web Tokens (JWTs). You generate these tokens on your server, transfer them back to the client device, and then use them for authentication using the signInWithCustomToken() method.

  1. You need to save the username and password in your database or rtdb or firestore
  2. When the user touches the login button, the client will send the username and password to your backend. If the username and password are correct, generate your own token and send it back to the client
  3. Then the client can log in using a user token from the server using the signInWithCustomToken() method

You can read more in this documentation.

+6
source

You can also use the Alfonso solution. And where you need a real email, you can set a text box for email when a user logs in, and you can save it in your database, and you can use it.

+2
source

All Articles