07-31-2023, 09:39 AM
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");
}
}
}}
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");
}
}
}}