-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignUpActivity.kt
105 lines (90 loc) · 4.15 KB
/
SignUpActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.example.emitracker
import android.content.Intent
import android.os.Bundle
import android.util.Patterns
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.FirebaseFirestore
class SignUpActivity : AppCompatActivity() {
private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore
private lateinit var emailSignUp: EditText
private lateinit var passwordSignUp: EditText
private lateinit var signUpButton: Button
private lateinit var signUpProgressBar: ProgressBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_up)
auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()
emailSignUp = findViewById(R.id.emailSignUp)
passwordSignUp = findViewById(R.id.passwordSignUp)
signUpButton = findViewById(R.id.signUpButton)
signUpProgressBar = findViewById(R.id.signUpProgressBar)
signUpButton.setOnClickListener {
val email = emailSignUp.text.toString().trim()
val password = passwordSignUp.text.toString().trim()
if (validateInput(email, password)) {
signUpProgressBar.visibility = View.VISIBLE
signUpButton.isEnabled = false
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
signUpProgressBar.visibility = View.GONE
signUpButton.isEnabled = true
if (task.isSuccessful) {
val userId = auth.currentUser?.uid
if (userId != null) {
val userData = hashMapOf(
"email" to email,
"createdAt" to System.currentTimeMillis()
)
db.collection("users")
.document(userId)
.set(userData)
.addOnSuccessListener {
Toast.makeText(this, "Sign-Up Successful", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, TaskActivity::class.java))
finish()
}
.addOnFailureListener { e ->
Toast.makeText(this, "Failed to create user data: ${e.message}", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "User ID not found.", Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Sign-Up Failed: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
}
}
}
}
}
private fun validateInput(email: String, password: String): Boolean {
if (email.isEmpty()) {
emailSignUp.error = "Email is required."
emailSignUp.requestFocus()
return false
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
emailSignUp.error = "Please enter a valid email."
emailSignUp.requestFocus()
return false
}
if (password.isEmpty()) {
passwordSignUp.error = "Password is required."
passwordSignUp.requestFocus()
return false
}
if (password.length < 6) {
passwordSignUp.error = "Password should be at least 6 characters."
passwordSignUp.requestFocus()
return false
}
return true
}
}