0

I'm trying to make an app with firebase (authentication) which allows a teacher to create his student's user, then send the student the username and password and let him log in. My problem is that in order to do so, after the teacher is creating the user, the current user is the student's, and when I'm trying to do the following code:

private void signStu(String email,String password)
    {
        final FirebaseUser teacher=mAuth.getCurrentUser();
        mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                FirebaseUser student=mAuth.getCurrentUser();
                User newUser=new User(/*data i need to pass to the constructor, doesn't affect the result*/);
                mAuth.updateCurrentUser(teacher);   //Here it passes on the student user and not the teacher
            }
        });
    }

So it seems like the mAuth.getCurrentUser() is updated to all of its usages before, so the teacher variable becomes the student variable after it's created. Anyone got any idea of how to fix that, or a way to work around that? Thanks !

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Adi Harel
  • 505
  • 1
  • 5
  • 10

1 Answers1

0

FirebaseAuth.getInstance().signOut(); To sign out the student. More info on what to do afterwards here: How to make a user sign out in Firebase?. You can try signing out the user and re-signing in. Maybe something something from the official firebase documentation such as re-authenticating a user could help. In this link, this example code is given:

AuthCredential credential = EmailAuthProvider
    .getCredential("user@example.com", "password1234"); //teacher info

// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential)
    .addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            Log.d(TAG, "User re-authenticated.");
        }
    });

Apparently you can also handle this server side if you'd like to approach it that way. Firebase kicks out current user

Alvin X.
  • 161
  • 1
  • 7
  • Thanks. It seems to be working out, just kinda weird firebase don't have a way to do this in one line of code... – Adi Harel Feb 09 '19 at 07:49
  • Yeah it's funny because they have an implementation for it but not on mobile it seems. – Alvin X. Feb 09 '19 at 17:51
  • Is there any easy way of finding out the password of the signed in user via authentication? – Adi Harel Feb 09 '19 at 23:27
  • For security reasons I don't believe it's possible. What are you trying to accomplish? – Alvin X. Feb 09 '19 at 23:36
  • I'm trying to re-sign in. What I'm doing now is saving the entered password from the log in screen as a static variable and using it wherever I need to re sign the teacher but it feels like it's not the elegant solution for that – Adi Harel Feb 10 '19 at 09:23
  • Saving that password in `SharedPreferences` may be more secure. There is an implementation to saving credentials called smart lock by Google which you might want to take a look at. Particularly, a library called `FirebaseUI Auth` which has smart lock built in which can apparently save and sign users in automatically. So maybe implementing this with the teacher and not the student might work. https://github.com/firebase/FirebaseUI-Android/blob/master/auth/README.md#authui-sign-in and https://developers.google.com/identity/smartlock-passwords/android/store-credentials – Alvin X. Feb 10 '19 at 09:44