Search code examples
javafirebasefirebase-realtime-databasefirebase-authenticationtoken

How to create unique token, in every users firebase android?


I want to generate and store token everytime users login. I tried for my code below, but it always generate same token when I login with another account.

mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        AlertDialog.dismiss();
                        if (!task.isSuccessful()) {
                            if (password.length() < 6) {
                                new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("Oops...")
                                        .setContentText("Enter minimum 6 charachters !! ")
                                        .show();
                            } else {
                                passwordInput.setText("");
                                new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("Oops...")
                                        .setContentText("Authentication failed !!")
                                        .show();
                            }
                        } else {



                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();
                            DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users/"+ users.getUid());
                            String token = FirebaseInstanceId.getInstance().getToken();

                            Log.e("tokenid",""+token);
                            mDatabase.child("token_id").setValue(token);
                            finish();
                        }
                    }
                });
            }
        });

please help, thanks..


Solution

  • When using the following line of code:

    String token = FirebaseInstanceId.getInstance().getToken();
    

    You are getting the Firebase Instance ID token, which is also known as FCM token, on the client side. You need to know that in Firebase there two different tokens:

    1. A FCM Token/Instance ID token that identifies an installation of a specific application on a specific user device. It doesn't identify in any way a particular user.

    2. An Auth ID token identifies a specific user of a specific application. It doesn't identify in any way a particular device.

    The two tokens are quite different and serve different purposes. Please see the official documentation for Android regarding on how to retrieve ID tokens on clients.

    The identity of the user is remembered indefinitely (or until you sign out). But their credentials (or rather, whether their token is still valid) are re-checked every hour.