-1

good night, I managed to create my first login in my application with kotlin, at the moment every time the app opens it asks you to log in, I would like to know how to do that when the user is logged in, it does not ask to log in again and finally the session closing so that now if the user asks to register again. This is my code:

package com.example.orashopboss

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.activity_main.*

enum class ProviderType {
    BASIC
}

class Login : AppCompatActivity() {
    private val mAuth : FirebaseAuth by lazy { FirebaseAuth.getInstance() }
    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        signIn.setOnClickListener {
            val email = editTextEmailLog.text.toString()
            val password = editTextPassLog.text.toString()
            if (isValidEmailAndPassword(email , password)) {
                signUpByEmail(email , password)
            } else {
                val intent = Intent(this , popUpWndow::class.java)
                startActivity(intent)
            }
        }
    }
    private fun signUpByEmail(email : String , password : String) {
        mAuth.signInWithEmailAndPassword(email , password).addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                if (mAuth.currentUser!!.isEmailVerified) {
                    val intent = Intent(this , home::class.java)
                    startActivity(intent)
                } else {
                    val intent = Intent(this , emailnoverified::class.java)
                    startActivity(intent)
                }
            }
        }
    }
    private fun isValidEmailAndPassword(email : String , password : String): Boolean {
        return !email.isNullOrEmpty() &&
                !password.isNullOrEmpty()
    }
}

Thank you and good night!

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • I think this **[answer](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth)** might help. – Alex Mamo Dec 09 '20 at 09:29

2 Answers2

3

You're supposed to use an auth state listener to get a callback when the previously signed in user object is first available. The user object won't be available when the app first launches, so use the callback to know when the SDK has loaded it.

See also:

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

As @Doug Stevenson already answered this question. here is the simple implementation in java.

Define globally

private FirebaseUser user;
private FirebaseAuth firebaseAuth;

oncreate

    firebaseAuth = FirebaseAuth.getInstance();
    user = firebaseAuth.getCurrentUser();

then

      if (user != null) {
            finish();
            startActivity(new Intent(LoginActivity.this, 
     MainActivity.class));
        }

So now in short whenever you open your app.everytime login will go through the condition if currentuser equals to null then app will show login screen if not then app will go to the next screen you mentioned.

Majid Ali
  • 485
  • 6
  • 17
  • Thank you, but when the user logs in and backs down (only the first time) they return to the login screen, and the second time when the user backs up they exit the app but the first time they don't. How do I fix that? – Liam Brown. Dec 09 '20 at 15:28
  • @LiamBrown.finish the login acitivty first then navigate to any other activity. – Majid Ali Dec 10 '20 at 08:08