Reputation: 1789
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");
}
}
}}
Upvotes: 3
Views: 1680
Reputation: 2148
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");
}
}
Upvotes: 1