Skip to content

Instantly share code, notes, and snippets.

@gavelez
Created April 24, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavelez/a55764382a99e510490b295aaaff8cbe to your computer and use it in GitHub Desktop.
Save gavelez/a55764382a99e510490b295aaaff8cbe to your computer and use it in GitHub Desktop.
package com.example.monpfe
import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnFailureListener
import com.google.firebase.auth.FirebaseAuth
class RegisterActivity : AppCompatActivity() {
lateinit var mCreateBtn: Button
lateinit var mSetEmail: EditText
lateinit var mSetPass: EditText
lateinit var mConfPass: EditText
lateinit var mPhone: EditText
lateinit var mName: EditText
lateinit var mAuth: FirebaseAuth
lateinit var mProgressbar: ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mCreateBtn = findViewById(R.id.create_btn)
mName = findViewById(R.id.user_name)
mSetEmail = findViewById(R.id.user_email)
mPhone = findViewById(R.id.user_phone)
mSetPass = findViewById(R.id.user_pass)
mConfPass = findViewById(R.id.conf_pass)
mAuth = FirebaseAuth.getInstance()
mProgressbar = ProgressDialog(this)
mCreateBtn.setOnClickListener {
val email = mSetEmail.text.toString().trim()
val password = mSetPass.text.toString().trim()
val name = mName.text.toString().trim()
val confpass = mConfPass.text.toString().trim()
val phone = mPhone.text.toString().trim()
if (TextUtils.isEmpty(email)) {
mSetEmail.error = "Enter Email"
return@setOnClickListener
}
if (TextUtils.isEmpty(password)) {
mSetPass.error = "Enter Password"
return@setOnClickListener
}
if (TextUtils.isEmpty(name)) {
mName.error = "Enter Your Name"
return@setOnClickListener
}
if (TextUtils.isEmpty(confpass) || (!confpass.equals(password))) {
mConfPass.error = "Check Your Password "
return@setOnClickListener
}
if (TextUtils.isEmpty(phone)) {
mPhone.error = "Enter Your Phone Number"
return@setOnClickListener
} else {
mProgressbar.setTitle("Create Account")
mProgressbar.setMessage("Please wait, while we are checking the credentials.")
mProgressbar.setCanceledOnTouchOutside(false)
mProgressbar.show()
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
mProgressbar.dismiss()
startActivity(Intent(this, LoginActivity::class.java))
finish()
} else {
Log.d("TAG", "Authentication failed.")
Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT)
.show()
}
}.addOnFailureListener(OnFailureListener {
Log.e("FAILURE", it.message)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment