Created
March 14, 2019 21:22
-
-
Save NezSpencer/5f829b5a24ddcdfd1d11d4a0915d2b36 to your computer and use it in GitHub Desktop.
Callback-based Firebase auth code. This allows user to sign up and login and also monitors the state of user authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.ProgressDialog | |
import android.databinding.DataBindingUtil | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.text.TextUtils | |
import android.view.View | |
import android.widget.Toast | |
import com.google.firebase.auth.FirebaseAuth | |
import com.nezspencer.testapp.databinding.ActivitySubBinding | |
class SubActivity : AppCompatActivity(), FirebaseAuth.AuthStateListener, View.OnClickListener{ | |
private lateinit var mAuth : FirebaseAuth | |
private lateinit var progress : ProgressDialog | |
private lateinit var binding : ActivitySubBinding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_sub) | |
progress = ProgressDialog(this) | |
progress.setCanceledOnTouchOutside(false) | |
progress.setCancelable(false) | |
binding.btnSignin.setOnClickListener(this) | |
binding.btnSignup.setOnClickListener(this) | |
mAuth = FirebaseAuth.getInstance() | |
} | |
override fun onStart() { | |
super.onStart() | |
mAuth.addAuthStateListener(this) | |
} | |
override fun onStop() { | |
super.onStop() | |
mAuth.removeAuthStateListener(this) | |
} | |
override fun onAuthStateChanged(p0: FirebaseAuth) { | |
if (progress.isShowing) | |
progress.hide() | |
if (p0.currentUser != null) { | |
//do some UI magic | |
Toast.makeText(this, "Auth successful", Toast.LENGTH_SHORT).show() | |
} | |
else { | |
// also do some UI unauthenticated magic | |
Toast.makeText(this, "Auth failed", Toast.LENGTH_SHORT).show() | |
} | |
} | |
override fun onClick(p0: View?) { | |
when(p0!!.id){ | |
R.id.btn_signup -> { | |
val email = binding.etEmail.text.toString() | |
val password = binding.etPwd.text.toString() | |
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) | |
signupUser(email, password) | |
} | |
R.id.btn_signin ->{ | |
val email = binding.etEmail.text.toString() | |
val password = binding.etPwd.text.toString() | |
if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) | |
signInUser(email, password) | |
} | |
} | |
} | |
private fun signupUser(email : String, password : String){ | |
if (!progress.isShowing) | |
progress.show() | |
mAuth.createUserWithEmailAndPassword(email, password) | |
} | |
private fun signInUser(email : String, password: String) { | |
if (!progress.isShowing) | |
progress.show() | |
mAuth.signInWithEmailAndPassword(email, password) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment