Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 422 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I know if a user already created an account while using firebase-ui on my android application?

#1
And here's the long version:

I'm totally new to Firebase and Android Studio, and I have already written up some parts like signing in with google and FB in my app. I followed [this tutorial]().

Everything works fine but what I want to do now is collect some info from user and store it in Firebase Database(which I think I've a basic idea of). But the problem is that I've no idea on how I can know if its the first time a user is creating the account, because I only want to ask these details once per account. So I thought I'd use the 'User UID' from Auth. But then, the problem I encountered was, how /where am I supposed to write the code to check if the user already exists when using the above mentioned tutorial. Am I supposed to do it in the rules section in the Database or in the code itself?

I'll post the code I made by following the tutorial. I'm sorry if this is a noob question, but none of the developers I know have experience with Firebase UI. So I don't really have anyone to ask about his. Any help is appreciated.

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.firebase.ui.auth.AuthUI;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {

private static final int RC_SIGN_IN=0;
private FirebaseAuth auth;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
if(auth.getCurrentUser() != null) {
//user already signed in

Log.d("AUTH", auth.getCurrentUser().getEmail());
Intent intent = new Intent(this, com.jacob.appdevitae.HomeActivity.class);
startActivity(intent);
finish();
}
else {
startActivityForResult(AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(
AuthUI.FACEBOOK_PROVIDER,
AuthUI.GOOGLE_PROVIDER)
.build(),RC_SIGN_IN);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN) {
if(resultCode == RESULT_OK) {
//User logged in
Log.d("AUTH", auth.getCurrentUser().getEmail());
Intent intent = new Intent(this, com.jacob.appdevitae.HomeActivity.class);
startActivity(intent);
finish();
}
else {
//User not authenticated
Log.d("AUTH", "NOT AUTHENTICATED");
}
}
}}
Reply

#2
First add the dependencie in the app level of gradle
com.google.firebase:firebase-database:9.6.1

The user only sign un once so you dont have to check if user exists, just add the information after signing up.

For example:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RC_SIGN_IN) {
if(resultCode == RESULT_OK) {
//User logged in
Log.d("AUTH", auth.getCurrentUser().getEmail());
//create the database
DatabaseReference mdatabase=FirebaseDatabase.getInstance.getReference.child("User-Information");

//set the information, you can put how much as you want
mdatabase.child("user-information1").setValue("User-information1");
mdatabase.child("user-information2").setValue("User-information2")
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(LOG_TAG, "signIn:onComplete:" + task.isSuccessful());

if (task.isSuccessful()) {
//success
} else {
//Error

}
}
});;

Intent intent = new Intent(this, com.jacob.appdevitae.HomeActivity.class);
startActivity(intent);
finish();
}
else {
//User not authenticated
Log.d("AUTH", "NOT AUTHENTICATED");
}
}


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through