Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Android Login/Register

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.example.myapplication

import android.content.Context
import android.content.Intent
import android.widget.Toast

fun Context.toast(message: String) =
       Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

fun Context.login(){
   val intent = Intent(this, HomeActivity::class.java).apply {
       flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
   }
       startActivity(intent)
}




---------------



package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : AppCompatActivity() {

   private lateinit var mAuth: FirebaseAuth

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_login)

       mAuth = FirebaseAuth.getInstance()

       loginBtn.setOnClickListener {

           val email = emailText.text.toString().trim()
           val pw = passwordText.text.toString().trim()

           if (email.isEmpty()){
               emailText.error = "Email required"
               emailText.requestFocus()
               return@setOnClickListener
           }
           if (pw.isEmpty()){
               passwordText.error = "Password Required"
               passwordText.requestFocus()
               return@setOnClickListener
           }

           loginUser(email, pw)

       }

       registerActivityText.setOnClickListener {
           startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))
       }
   }

   private fun loginUser(email: String, pw: String) {
       progressBar2.visibility = View.VISIBLE

       mAuth.signInWithEmailAndPassword(email, pw)
           .addOnCompleteListener(this){task ->
               progressBar2.visibility = View.INVISIBLE
               if (task.isSuccessful){

                   login()


               } else{
                   task.exception?.message?.let {
                       toast(it)
                   }
               }
           }
   }

   override fun onStart() {
       super.onStart()

       mAuth.currentUser?.let {
           login()
           }

   }

}



---------------------

package com.example.myapplication

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity : AppCompatActivity() {

   private lateinit var mAuth : FirebaseAuth

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_register)

       mAuth = FirebaseAuth.getInstance()

       registerBtn.setOnClickListener {
           val email = emailText2.text.toString().trim()
           val pw = passwordText2.text.toString().trim()

           if (email.isEmpty()){
               emailText2.error = "Email required!"
               emailText2.requestFocus()
               return@setOnClickListener
           }
           if (pw.isEmpty()){
               passwordText2.error = "Password required!"
               passwordText2.requestFocus()
               return@setOnClickListener
           }

           registerUser(email, pw)
       }

       loginActivityText.setOnClickListener {
           startActivity(Intent(this@RegisterActivity, LoginActivity::class.java))
       }
   }

   private fun registerUser(email: String, pw: String) {
       progressBar2.visibility = View.VISIBLE
       mAuth.createUserWithEmailAndPassword(email, pw)
           .addOnCompleteListener(this) {task ->
               progressBar2.visibility = View.INVISIBLE
               if (task.isSuccessful){

                   login()
               }else{
                   task.exception?.message?.let {
                       toast(it)
                   }
               }
           }

   }

   override fun onStart() {
       super.onStart()

       mAuth.currentUser?.let {
               login()

       }
   }
}



 
Saloon Keeper
Posts: 5696
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a question about this code?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!