Здравствуйте и спасибо за ваше время.
Я работаю над приложением для Android с использованием Firebase.
Я настроил Аутентификацию Firebase, и пользователь может зарегистрироваться по электронной почте и паролю.и войдите после подтверждения электронной почты.
Когда приложение открывается, я проверяю, вошел ли пользователь в систему с помощью метода onStart()
, но я попытался сделать это после удаления пользователя из firebase, и я все еще могу войти!
Должен ли я проверить это по-другому?
@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
updateUI(currentUser);
}
******************************************* ОБНОВИТЬ *********************************************
Исправлена проблема с использованием AuthStateListener , но затем я разделил методы signIn и createAccount на 2 отдельных действия.Я также отделил createAccount()
от signInWithEmailAndPassword()
методов, что заставило меня добавить этот mAuth = FirebaseAuth.getInstance()
в оба метода onCreate()
.В логине я добавил
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser currentUser = mAuth.getCurrentUser();
...
}
};
, но теперь не работает.Я что-то забыл или просто не могу этого сделать?
Вот код, который я нашел релевантным:
Класс LogInActivity onCreate ():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
// Views
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.pswdEditText);
// Buttons
findViewById(R.id.logInButton).setOnClickListener(this);
findViewById(R.id.forgottenPaswdTextButton).setOnClickListener(this);
findViewById(R.id.registerTextButton).setOnClickListener(this);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// Check for user connection
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
Log.d(TAG, "onAuthStateChanged:signed_in:" + currentUser.getUid());
} else {
Log.d(TAG, "onAuthStateChanged:signed_out");
}
updateUI(currentUser);
}
};
}
Класс SignInActivity onCreate ():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
// Views
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.pswdEditText);
passwordRetypedEditText = findViewById(R.id.pswdRetypeEditText);
nameEditText = findViewById(R.id.nameEditText);
// Buttons
findViewById(R.id.signUpButton).setOnClickListener(this);
findViewById(R.id.logInTextButton).setOnClickListener(this);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}