mercredi 7 juillet 2021

How to add "Remember me" in login activity? [duplicate]

I have login acitvity and updateUI function that allows you not to log in, if the user has already logged in, but i need to add "Remember me" checkbox that allows you to choose whether to remember the session or not. How can i do this?

LoginActivity code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityLoginBinding.inflate(layoutInflater)
    setContentView(binding.root)

    initDatabase()
    updateUI()
    loginUser()

    binding.btnSignup.setOnClickListener {
        val intent = Intent(this, Signup::class.java)
        startActivity(intent)
        finish()
    }

    binding.btnContinueWithoutLogin.setOnClickListener {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }
}

private fun loginUser() {
    binding.btnLogin.setOnClickListener {
        val email = binding.etEmail.text.toString().trim()
        val password = binding.etPassword.text.toString().trim()

        if (email.isEmpty()) {
            binding.etEmail.error = "Field can not be empty"
            binding.etEmail.requestFocus()

            return@setOnClickListener
        }

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            binding.etEmail.error = "Provide valid email"
            binding.etEmail.requestFocus()

            return@setOnClickListener
        }

        if (password.isEmpty()) {
            binding.etPassword.error = "Field can not be empty"
            binding.etPassword.requestFocus()

            return@setOnClickListener
        }

        if (password.length < 8) {
            binding.etPassword.error = "Minimal password length should be 8"
            binding.etPassword.requestFocus()

            return@setOnClickListener
        }

        mAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    val intent = Intent(this, MainActivity::class.java)
                    startActivity(intent)
                    finish()
                } else {
                    Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show()
                }
            }
    }
}

private fun updateUI() {
    val currentUser = mAuth.currentUser

    if (currentUser != null) {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }
}


private fun initDatabase()
{
    mAuth = FirebaseAuth.getInstance()
}

MainActivity code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    mAuth = FirebaseAuth.getInstance()

    binding.btnLogout.setOnClickListener {
        mAuth.signOut()

        val intent = Intent(this, Login::class.java)
        startActivity(intent)
        finish()
    }
}

I tried to use Sharedpreferences, but somehow it didn't work out




Aucun commentaire:

Enregistrer un commentaire