When I click the register button, it does not save the data and gives the following error;
com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The email address is badly formatted
Please help us find the error!
Register code page;
class RegisterActivity : AppCompatActivity() {
private lateinit var binding : ActivityRegisterBinding
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityRegisterBinding.inflate(layoutInflater)
setContentView(binding.root)
firebaseAuth = FirebaseAuth.getInstance()
binding.registerButton.setOnClickListener {
val email = binding.yourEmail.text.toString()
val pass = binding.editTextTextPassword2.text.toString()
val username = binding.editTextTextPersonName.text.toString()
if(email.isNotEmpty() && pass.isNotEmpty() && username.isNotEmpty()){
firebaseAuth.createUserWithEmailAndPassword(email , pass).addOnCompleteListener{
if (it.isSuccessful){
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
}else{
Toast.makeText(this,it.exception.toString(), Toast.LENGTH_LONG).show()
}
}
}else{
Toast.makeText(this,"Empty Fields Are not Allowed !!", Toast.LENGTH_LONG).show()
}
}
val loginresetActivity = findViewById<TextView>(R.id.textView6)
textView6.setOnClickListener {
val Intent = Intent(this, LoginActivity::class.java)
startActivity(Intent)
}
}
}
I don't know if this will help but Make sure the string doesn't have white spaces im both sides (left and right)
val email = binding.yourEmail.text.toString().trim()
Related
My Authentication is working fine but I am not able to store and retrive data from realtime database. Below is the firebase rule(I am using locked mode and Usa server) and my signup Activity class. Please help me with this thing. I am taking database refernce and giving setValue method but still its not working. I have also tried different solution given on stackoverflow flow but still no luck.
Firebase Rule
{
"rules": {
".read": "true", // 2022-8-22
".write": "true"
}
}
SignUp Acitvity
class SignUpActivity : AppCompatActivity() {
private lateinit var mAuth : FirebaseAuth
private lateinit var mDBRef : DatabaseReference
private lateinit var binding: ActivitySignIntoBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignIntoBinding.inflate(layoutInflater)
setContentView(binding.root)
mAuth = FirebaseAuth.getInstance()
binding.edtSignUp.setOnClickListener{
val name = binding.edtName.text.toString()
val email = binding.edtEmail.text.toString()
val password = binding.edtPassword.text.toString()
signUp(name, email, password)
}
binding.tvLogin.setOnClickListener {
val intent = Intent(this, Login::class.java)
finish()
startActivity(intent)
}
}
private fun signUp(name: String, email: String, password: String) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
addUserToDatabase(name, email, mAuth.currentUser?.uid!!)
val intent = Intent(this#SignUpActivity, MainActivity::class.java)
startActivity(intent)
} else {
Toast.makeText(this#SignUpActivity, "Some Error Occurred. Try Again", Toast.LENGTH_LONG ).show()
}
}
}
private fun addUserToDatabase(name: String, email: String, uid: String) {
mDBRef = FirebaseDatabase.getInstance("https://chatapplication-ad542-default-rtdb.firebaseio.com/").getReference()
mDBRef.child("User").child(uid).push().setValue(User(name, email, uid))
}
}
My title is not displaying in the app.
I think there is a problem with the intent.
So, my main file is the login page and the code is:
class MainActivity : AppCompatActivity()
{
lateinit var username: EditText
lateinit var password1: EditText
lateinit var logIn: Button
lateinit var signup: TextView
lateinit var forgotpassword: TextView
val user="DiyaK"
val pass=arrayOf("Diya#2826","Sidd#2826","Prat#2826","Prash#2826")
var titlename: String? = "DiyaK"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scroll)
println("Create called")
title = "Login Page"
username=findViewById(R.id.username)
password1=findViewById(R.id.password1)
logIn=findViewById(R.id.logIn)
signup=findViewById(R.id.signup)
forgotpassword=findViewById(R.id.forgotpassword)
logIn.setOnClickListener {
val user1= username.text.toString()
val pass1=password1.text.toString()
var name="Diya K"
if(user1==user) {
if (pass1 == pass[0]) {
val intent = Intent(this#MainActivity, NewActivity::class.java)
startActivity(intent)
name="Diya K"
intent.putExtra("Name",name)
} else if (pass1 == pass[1]) {
val intent2 = Intent(this#MainActivity, NewActivity6::class.java)
startActivity(intent2)
name="Siddhant K"
intent2.putExtra("Name",name)
} else if (pass1 == pass[2]) {
val intent3 = Intent(this#MainActivity, NewActivity3::class.java)
startActivity(intent3)
name="Pratibha K"
intent3.putExtra("Name",name)
} else if (pass1 == pass[3]) {
val intent4 = Intent(this#MainActivity, NewActivity4::class.java)
startActivity(intent4)
name="Prashant K"
intent4.putExtra("Name",name)
}
} else {
Toast.makeText(this#MainActivity, "Incorrect! Try again", Toast.LENGTH_SHORT)
.show()
}
}
}
}
And, I have created 4 new activities, like for each different page and the code for all are similar, so I am showing the code for one only.
class NewActivity : AppCompatActivity() {
lateinit var img: ImageView
lateinit var state: TextView
lateinit var btn: Button
lateinit var btn1: Button
lateinit var header: TextView
lateinit var subheader: TextView
lateinit var hobby: TextView
var title1: String? = "Diya K"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new1)
img = findViewById(R.id.img)
state = findViewById(R.id.state)
btn = findViewById(R.id.btn)
btn1 = findViewById(R.id.btn1)
header = findViewById(R.id.header)
subheader = findViewById(R.id.subheader)
hobby=findViewById(R.id.hobby)
btn1.setOnClickListener {
Toast.makeText(this#NewActivity, "Hurray", Toast.LENGTH_SHORT).show()
val intent1 = Intent(this#NewActivity, MainActivity::class.java)
startActivity(intent1)
}
if(intent != null) {
title1=intent.getStringExtra("Name")
}
title=title1
}
}
My title is not displaying in the app. I think there is a problem with the intent.
The problem is you are setting the title after starting the activity
startActivity(intent)
name="Diya K"
intent.putExtra("Name",name)
Solution is set the extras first before starting the activity. Just swap the lines like below:
name="Diya K"
intent.putExtra("Name",name)
startActivity(intent)
if(intent != null) {
title1=intent.getStringExtra("Name")
}
title=title1
header.setText(title)// you get value but not set
i have 3 progress activities for signup. I take the informations at first activity and then trying to send directly third activity to signup with firebase. I tried every way that i research but i can't figure it out.
This is my first activity
signUpNextButton.setOnClickListener {
val name = signUpFullname.text.toString()
val email = signUpEmail.text.toString()
val password = signUpPassword.text.toString()
val repassword = signUpPasswordRepeat.text.toString()
//Sending datas with this method
val intent = Intent(this, SignUp_Page3::class.java)
intent.putExtra("user_email", email)
intent.putExtra("user_password", password)
startActivity(Intent(this, SignUp_Page2::class.java))
}
this is the second one
signUpGetCode.setOnClickListener {
startActivity(Intent(this, SignUp_Page3::class.java)) }
and this is the last one:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup3)
val intent = getIntent()
val email = intent.getStringExtra("user_email")
val password = intent.getStringExtra("user_password")
signUpComplete.setOnClickListener {
//Firebase Authentication to create user
FirebaseAuth.getInstance()
.createUserWithEmailAndPassword(
email,
password
)
.addOnCompleteListener {
if (it.isSuccessful) {
startActivity(Intent(this, LogIn_Page::class.java))
Toast.makeText(
this,
"Account successfully created, please log in.",
Toast.LENGTH_LONG
).show()
finish()
} else {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_SHORT).show()
return#addOnCompleteListener
}
}
}
It looks like you are passing to the second page via Intent, but never pass from second to third page.
First retrieve the info in your second and third page.
private var email: String? = null
private var password: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent != null) {
email = intent.getStringExtra("user_email")
password = intent.getStringExtra("user_password")
} else if (savedInstanceState != null){
email = savedInstanceState.getString("user_email")
password = savedInstanceState.getString("user_password")
}
//TODO rest of onCreate
}
Try passing the info into the third page Intent.
Be careful about storing this data in the Intent. If you don't implement onSaveInstanceState in your second and third page, then you are going to lose the data on a rotation.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("user_email", email)
outState.putString("user_password", password)
}
Also consider using Static String constants for your keys it will make everything much more manageable.
First1:
signUpNextButton.setOnClickListener {
val name = signUpFullname.text.toString()
val email = signUpEmail.text.toString()
val password = signUpPassword.text.toString()
//Sending datas with this method
val intent = Intent(this, SignUp_Page2::class.java)
intent.putExtra("user_email", email)
intent.putExtra("user_password", password)
//Starting another activity
startActivity(intent)
}
second1:
private var email: String? = null
private var password: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup2)
val intent = getIntent()
if (intent != null) {
email = intent.getStringExtra("user_email")
password = intent.getStringExtra("user_password")
} else if (savedInstanceState != null){
email = savedInstanceState.getString("user_email")
password = savedInstanceState.getString("user_password")
}
signUpGetCode.setOnClickListener {
val phone = signUpPhoneNumber.text.toString()
startActivity(Intent(this, SignUp_Page3::class.java))
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("user_email", email)
outState.putString("user_password", password)
}
third1:
private var email: String? = null
private var password: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.signup3)
val intent = getIntent()
if (intent != null) {
email = intent.getStringExtra("user_email")
password = intent.getStringExtra("user_password")
} else if (savedInstanceState != null) {
email = savedInstanceState.getString("user_email")
password = savedInstanceState.getString("user_password")
}
signUpComplete.setOnClickListener {
Toast.makeText(this, email + password, Toast.LENGTH_SHORT).show()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("user_email", email)
outState.putString("user_password", password)
}
Toast message give me the null null output.
FirebaseAuth.getInstance()
.createUserWithEmailAndPassword(
email.toString(),
password.toString()
)
.addOnCompleteListener {
if (it.isSuccessful) {
startActivity(Intent(this, LogIn_Page::class.java))
Toast.makeText(
this,
"Account successfully created, please log in.",
Toast.LENGTH_LONG
).show()
} else {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_SHORT).show()
return#addOnCompleteListener
}
}
this returns "something went wrong" to me
I have created an app in Android Studio (Kotlin) that registers users:
User Interface
An email/password user is created (Authentication) along with a corresponding database record (Realtime Database):
Datbase Structure
I have the login procedure working and I'm able to display the logged-in user's email address on MainActivity, BUT, I don't know how to display the user's name and favourite colour (from the database).
Can anyone help?
Thanks in advance for your time.
RegisterActivity.kt
class RegisterActivity : AppCompatActivity() {
private lateinit var mAuth : FirebaseAuth
private lateinit var viewModel: UserViewModel
public val USER: String = "user"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mAuth = FirebaseAuth.getInstance()
viewModel = ViewModelProvider(this).get(UserViewModel::class.java)
register_button.setOnClickListener {
val email = register_email.text.toString().trim()
val password = register_password.text.toString().trim()
val name = register_name.text.toString().trim()
val colour = register_colour.text.toString().trim()
mAuth.createUserWithEmailAndPassword(email, password)
val user = User()
user.email = email
user.name = name
user.colour = colour
viewModel.addUser(user)
}
}
}
LoginActivity.kt
class LoginActivity : AppCompatActivity() {
private lateinit var mAuth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
mAuth = FirebaseAuth.getInstance()
login_button.setOnClickListener {
val email = login_email.text.toString().trim()
val password = login_password.text.toString().trim()
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
//Login Success
val intent = Intent (this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)
}else{
//Login Failure
task.exception?.message?.let {
toast(it)
}
}
}
}
}
}
MainActivity.kt (to display User details)
class MainActivity : AppCompatActivity() {
private val currentUser = FirebaseAuth.getInstance().currentUser
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
currentUser?.let { user ->
main_email.setText(user.email)
main_name.setText("Don't Know")
main_colour.setText("Don't Know")
}
}
}
Edit 3
LoginActivity.kt
class LoginActivity : AppCompatActivity() {
private lateinit var mAuth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
mAuth = FirebaseAuth.getInstance()
login_button.setOnClickListener {
val email = login_email.text.toString().trim()
val password = login_password.text.toString().trim()
loginUser (email, password)
}
}
private fun loginUser(email: String, password: String) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
//Login Success
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("username",user.displayName)
intent.putExtra("email",user.email)
startActivity(intent)
}else{
//Login Failure
task.exception?.message?.let {
toast(it)
}
}
}
}
override fun onStart() {
super.onStart()
mAuth.currentUser?.let {
login()
}
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
//I have commented out the line below because it may not be required...
// private val currentUser = FirebaseAuth.getInstance().currentUser
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//I have commented out the lines below because they may not be required...
//currentUser?.let { user ->
//main_email.setText(user.email)
//main_name.setText("Don't Know")
//main_colour.setText("Don't Know")
//}
}
}
ERROR 1 : Overload resolution ambiguity on putExtra
ERROR 2 : Unresolved Reference on user.
When you launch at your login stage
mAuth.signInWithEmailAndPassword(email, password)
It has a callback that will be completed and successful when the user has been logged in, inside that callback, you are already logged in and that user now corresponds to one unique user ID, you can access that user ID by doing
val user = FirebaseAuth().getInstance().currentUser.uid
Then, here you can fetch that user data and pass it as an extra or bundle to your MainActivity, so the login process will take the time to log the user, fetch its data and send it to the MainActivity, this way you are not delaying any more time the user at your MainActivity
You can see that the user has been created once that successful callback is launch, and then in the Firebase console you can check for that user in the authentication tab
Edit
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
//Login Success, here you can get currentUser
val user = FirebaseAuth().getInstance().currentUser
login(user.displayName,user.email)
}else{
//Login Failure
task.exception?.message?.let {
toast(it)
}
}
}
Then, just add these two parameters to the login() method and just do a putExtra with those values and send those values as an extra to the main activity, so you are not fetching in your mainactivity and just sending the values from the login
Keep in mind that after .addonCompleteListener() the result is your current logged in user, so from here, you can get the currentUser information or the currentUser id
Edit 2
Inside the onComplete , just pass the data to the MainActivity
val intent = Intent(this,MainActivity::class.java)
intent.putExtra("username",user.displayName)
intent.putExtra("email",user.email)
startActivity(intent)
Then in your MainActivity get this extras
val extras = getIntent().extras
val userName: String?
val email: String?
if (extras != null) {
userName = extras.getString("userame")
email = extras.getString("email")
}
Then you already have the data from login into your MainActivity
Edit 3
To change the color, just change it from the MainActivity
your_text_view.setTextColor(ContextCompat.getColor(this,R.color.yourColor)
Please read this
I tried to get the data from the firebase cloud database, but the data didn't show out.
This is the code I try to retrieve data
private fun updateUI(currentUser: FirebaseUser?){
if(currentUser!=null){
fstore.collection("users")
.document(auth.currentUser!!.uid)
.get()
.addOnCompleteListener { task->
task.result!!.get("name") == name.text.toString();
task.result!!.get("email") == email.text.toString();
Toast.makeText(baseContext, "Haha", Toast.LENGTH_LONG).show()
}
}else{
Toast.makeText(baseContext, "fail", Toast.LENGTH_LONG).show()
}
}
Calling UpdateUI() form activity
class MainActivity : AppCompatActivity() {
lateinit var name: TextView
lateinit var email: TextView
private lateinit var fstore: FirebaseFirestore
private lateinit var auth : FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
name = findViewById(R.id.name)
email = findViewById(R.id.email)
auth = FirebaseAuth.getInstance()
fstore = FirebaseFirestore.getInstance()
updateUI(auth.currentUser)
}
firebase cloud database:
android simulator after I tried:
Your document id assigning is wrong
.document(doucumentID) //Not user ID
//Here your document id is
//JXoTcqxIVENTSF2.....
//As I can see your firestore image
and you are assigning data in a wrong way to TextView
name.text = "value you want to assign"
So edit your code like this
private fun updateUI(currentUser: FirebaseUser?){
if(currentUser!=null){
fstore.collection("users")
.document("JXoTcqxIVENTSF2..")
//can not see full id in image so I use '....'
//replace it with full id from firestore
.get()
.addOnCompleteListener { task->
name.text = task.result!!.get("name").toString()
email.text = task.result!!.get("email").toString()
Toast.makeText(baseContext, "Haha", Toast.LENGTH_LONG).show()
}
}else{
Toast.makeText(baseContext, "fail", Toast.LENGTH_LONG).show()
}
}