Firebase autenthication returns null in Android app - android

I'm writing an app that is supposed to connect to firebase database. Everything works fine, except the fact that when I check autenthication key (using getAuth() method), it always returns null. I use facebook for autenthication in my app, and I wanted to turn on autenthication on my database with:
{
"rules": {
".read": "auth != null",
".write":"auth != null"
}
}
My project has 2 classes, one is LoginActivity, that allows user to create account in database using Facebook account:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
firebaseAutenthication = FirebaseAuth.getInstance();
//Get current user ID
FirebaseUser mUser = firebaseAutenthication.getCurrentUser();
if (mUser != null) {
// Club is signed in
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String uid = firebaseAutenthication.getCurrentUser().getUid();
String image = firebaseAutenthication.getCurrentUser().getPhotoUrl().toString();
intent.putExtra("user_id", uid);
if (image != null || !Objects.equals(image, "")) {
intent.putExtra("profile_picture", image);
}
startActivity(intent);
finish();
Log.e(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
}
//Setup firebase autenthication listener
firebaseAutenthicatorListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mUser = firebaseAuth.getCurrentUser();
if (mUser != null) {
// Club is signed in
Log.e(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
} else {
// Club is signed out
Log.e(TAG, "onAuthStateChanged:signed_out");
}
}
};
//Initialize facebook SDK
FacebookSdk.sdkInitialize(getApplicationContext());
if (BuildConfig.DEBUG) {
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
}
//Create callback manager for facebook login
//onSucces calls login method
//TODO: onCancel and OnError should display popup with information that login failed
callbackManagerFromFacebook = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(callbackManagerFromFacebook, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "facebook:onSuccess:" + loginResult);
signInWithFacebook(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.e(TAG, "facebook:onCancel");
}
#Override
public void onError(FacebookException error) {
Log.e(TAG, "facebook:onError", error);
}
});
}
#Override
protected void onStart() {
super.onStart();
firebaseAutenthication.addAuthStateListener(firebaseAutenthicatorListener);
}
#Override
public void onStop() {
super.onStop();
if (firebaseAutenthicatorListener != null) {
firebaseAutenthication.removeAuthStateListener(firebaseAutenthicatorListener);
}
}
//FaceBook
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManagerFromFacebook.onActivityResult(requestCode, resultCode, data);
}
private void signInWithFacebook(AccessToken token) {
Log.e(TAG, "signInWithFacebook:" + token);
showProgressDialog();
credential = FacebookAuthProvider.getCredential(token.getToken());
firebaseAutenthication.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.e(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.e(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
String uid = task.getResult().getUser().getUid();
String name = task.getResult().getUser().getDisplayName();
String email = task.getResult().getUser().getEmail();
Uri imageUri = task.getResult().getUser().getPhotoUrl();
String image = "";
if(imageUri != null) {
image = imageUri.toString();
}
//Create a new User and Save it in Firebase database
User user = new User(uid, name, null, email, null, null, "About Me");
firebaseUsers.child(uid).setValue(user);
//Start MainActivity and pass user data to it
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("user_id", uid);
intent.putExtra("profile_picture", image);
startActivity(intent);
finish();
}
}
});
And 2nd one is MainActivity, supposed to get user information form his facebook profile and put them into the database:
void getValueFromFirebase() {
Log.e(TAG, "Started getValueFromFirebase()");
firebase.child(userID).child("name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.toString();
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
}
#Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(), firebaseError.toString(), Toast.LENGTH_LONG).show();
}
});
}
void createFirebaseConnection() {
this.firebase = new Firebase(AppConstants.FIREBASE_URL_USERS);
this.firebaseAuthentication = FirebaseAuth.getInstance();
this.firebaseUser = firebaseAuthentication.getCurrentUser();
//Create listener for firebase autenthication
//Log if user is disconnected
this.firebaseAutenthicationStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.e(TAG, "onAuthStateChanged:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged: user is not signed in!");
}
}
};
firebaseAuthentication.addAuthStateListener(this.firebaseAutenthicationStateListener);
//Check if auth is done
//If not, autenthicate with Facebook Token
if(this.firebase.getAuth() == null) {
Log.e(TAG, "firebase.getAuth() == null, starting authWithFacebook()...");
authWithFacebook();
}
}
void authWithFacebook() {
Log.e(TAG, "authWithFacebook() started!");
AccessToken accessToken = AccessToken.getCurrentAccessToken();
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
Log.e(TAG, "Facebook Token: " + accessToken.getToken());
Log.e(TAG, "Auth: " + firebase.getAuth());
}
void readDataFromLoginActivity() {
//Get the uid for the currently logged in Club from intent data passed to this activity
this.userID = getIntent().getExtras().getString("user_id");
//Get the imageUrl for the currently logged in Club from intent data passed to this activity
//Load image to ImageView
this.userImageUrl = getIntent().getExtras().getString("profile_picture");
}
#Override
public void onBackPressed() {
if (userProfile.moveState == 1) {
userProfile.slideBottom();
} else {
super.onBackPressed();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate() called...");
setContentView(R.layout.activity_main);
lay = (RelativeLayout) findViewById(R.id.lay);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
Log.e(TAG, "readDataFromLoginActivity() called...");
readDataFromLoginActivity();
Log.e(TAG, "createFirebaseConnection() called...");
createFirebaseConnection();
}
#Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart() called...");
addTopTabBar();
addProfileFragment();
addLeftSideBar();
Log.e(TAG, "Binding UI Elements...");
bindUIElements();
getValueFromFirebase();
}
Yet, the toast I display always shows "permission Error". How can I fix this?

You are using both the legacy 2.x.x SDK and the new 10.x.x SDK in the same app. This is not good practice and likely the source of the problem. Remove this line from your dependencies and make the needed code changes to use only the capabilities of the new SDK.
compile 'com.firebase:firebase-client-android:2.x.x'
The package names of the new SDK start with com.google.firebase. Those of the legacy SDK start with com.firebase.client.
Helpful tips are in the Upgrade Guide.

Related

Android Firebase Facebook Authentication Logout function

I am using the facebook auth medthod with firebase and I can't figure out how to properly Log out a user. After I press the 'Continue with Facebook' button and give it access to my profile the button changes in 'Log out' and shows a dialog when I click it. The problem is that it doesn't actually log me out and the state is still signed in.
I found here how to log out the user from Firebase and Facebook in my app but I can't figure out where to put those 2 lines. I am looking for a Logout function or something like that.
I found here (different here) what might be my solution but it's pretty messy and can't understand. Can you please help me?
#Override
protected void onCreate(Bundle savedInstanceState) {
mAuth = FirebaseAuth.getInstance();
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_login);
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.e(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.e(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.e(TAG, "facebook:onError", error);
// ...
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
DatabaseReference myRef = database.getReference("Users");
myRef.addValueEventListener(new ValueEventListener() {
boolean userExists = false;
User userFirebase;
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot mydata : dataSnapshot.getChildren()){
userFirebase = mydata.getValue(User.class);
if(userFirebase.getEmail().equals(user.getEmail())){
Log.e(TAG, "User found in the database");
userExists = true;
Intent intent = new Intent(getBaseContext(), ActivityMain.class);
startActivity(intent);
break;
}
}
if (!userExists){
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").push().setValue(new User(user.getDisplayName(),user.getEmail(),0,0));
Intent intent = new Intent(getBaseContext(), ActivityMain.class);
startActivity(intent);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
}); // checks if the user is in database and writes him if not
} else {
// User is signed out
Log.e(TAG, "onAuthStateChanged:signed_out");
}
}
};
}
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.e(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.e(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.e(TAG, "signInWithCredential", task.getException());
Toast.makeText(getBaseContext(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
You need to implement your own button and add and OnClickListener with the 2 methods mentioned :
Button logoutButton = (Button) findViewById(R.id.logout_button);
logoutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
LoginManager.getInstance().logOut();
}
});
You can't use the facebook logout button , because it only logs you out of facebook and you need the firebase logout aswell.

Facebook authentication in firebase not working

What I'm trying to do is login with Facebook using Firebase and then get name, email, profile picture and uid and then store it to Firebase Database.
Everything is working fine until clicking on login button and then the Facebook account window pop up. After that, when I select an account by clicking "Continue With Rishabh", nothing happens.
No authentication, no error, nothing. Same Facebook account selection window stays on screen and nothing happens.
Any help will be appreciated.
Here is my SignInActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
mSignInToolbar = (Toolbar) findViewById(R.id.signInToolbar);
setSupportActionBar(mSignInToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mRef = FirebaseDatabase.getInstance().getReference().child("Users");
mRef.keepSynced(true);
mEmailField = (EditText) findViewById(R.id.emailField);
mPasswordField = (EditText) findViewById(R.id.passowrdField);
mSigninBtn = (Button) findViewById(R.id.signinBtn);
mProgress = new ProgressDialog(this);
// Initialize Facebook Login button
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
}
#Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result back to the Facebook SDK
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
mProgress.setMessage("Logging in...");
mProgress.show();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
mProgress.dismiss();
}else{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
DatabaseReference childRef = mRef.child(uid);
childRef.child("name").setValue(name);
childRef.child("email").setValue(email);
childRef.child("image").setValue(image);
mProgress.dismiss();
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);
}
}
});
}
See what all you need to do is to change some permission in facebook developer page. See the below picture make sure you too make required permission "Yes" as I did :
FacebookSdk.sdkInitialize(getApplicationContext());
Put this in the Application.
I've solved my problem by running the application in a real device (not in the emulator).

Facebook Login via Firebase - Access_Token_Denied Error

Facebook logs in correctly. In the Firebase console, I can see the user has logged. However, I cannot access any of the user's info.
I am getting below error log in LogCat:
signInWithFacebook:{AccessToken token:ACCESS_TOKEN_REMOVED
permissions:[public_profile, contact_email, user_friends, email]}
Afterwards, I also receive a FirebaseError: Permission denied in the LogCat and as a toast inside the app.
Following this tutorial, I have made a project that integrates Facebook and Firebase. I've tried copy and pasting the source code, and replacing the necessary lines (such as Facebook ID and Firebase URL) but it isn't working for me.
here is the specific void
private void signInWithFacebook(AccessToken token) {
Log.d(TAG, "signInWithFacebook:" + token.getToken());
showProgressDialog();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}else{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
//Create a new User and Save it in Firebase database
User user = new User(uid,name,null,email,name);
mRef.child(uid).setValue(user);
Log.d(TAG, uid);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("user_id",uid);
intent.putExtra("profile_picture",image);
startActivity(intent);
finish();
}
hideProgressDialog();
}
});
}
here is the full code
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "AndroidBash";
public User user;
private EditText email;
private EditText password;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog mProgressDialog;
private DatabaseReference mDatabase;
//Add YOUR Firebase Reference URL instead of the following URL
private Firebase mRef=new Firebase("https://firebase.firebaseio.com");
//FaceBook callbackManager
private CallbackManager callbackManager;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDatabase = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String uid = mAuth.getCurrentUser().getUid();
String image=mAuth.getCurrentUser().getPhotoUrl().toString();
intent.putExtra("user_id", uid);
if(image!=null || image!=""){
intent.putExtra("profile_picture",image);
}
startActivity(intent);
finish();
Log.d(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
}
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mUser = firebaseAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
//FaceBook
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
signInWithFacebook(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
//
}
#Override
protected void onStart() {
super.onStart();
email = (EditText) findViewById(R.id.edit_text_email_id);
password = (EditText) findViewById(R.id.edit_text_password);
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
//FaceBook
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
//
protected void setUpUser() {
user = new User();
user.setEmail(email.getText().toString());
user.setPassword(password.getText().toString());
}
public void onSignUpClicked(View view) {
Intent intent = new Intent(this, SignUpActivity.class);
startActivity(intent);
}
public void onLoginClicked(View view) {
setUpUser();
signIn(email.getText().toString(), password.getText().toString());
}
private void signIn(String email, String password) {
Log.d(TAG, "signIn:" + email);
if (!validateForm()) {
return;
}
showProgressDialog();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String uid = mAuth.getCurrentUser().getUid();
intent.putExtra("user_id", uid);
startActivity(intent);
finish();
}
hideProgressDialog();
}
});
//
}
private boolean validateForm() {
boolean valid = true;
String userEmail = email.getText().toString();
if (TextUtils.isEmpty(userEmail)) {
email.setError("Required.");
valid = false;
} else {
email.setError(null);
}
String userPassword = password.getText().toString();
if (TextUtils.isEmpty(userPassword)) {
password.setError("Required.");
valid = false;
} else {
password.setError(null);
}
return valid;
}
private void signInWithFacebook(AccessToken token) {
Log.d(TAG, "signInWithFacebook:" + token.getToken());
showProgressDialog();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}else{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
//Create a new User and Save it in Firebase database
User user = new User(uid,name,null,email,name);
mRef.child(uid).setValue(user);
Log.d(TAG, uid);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("user_id",uid);
intent.putExtra("profile_picture",image);
startActivity(intent);
finish();
}
hideProgressDialog();
}
});
}
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}

Firebase / Facebook - How to Log Off / Sign Off?

I am stuck in a loop here.
Users can log in with their FB account. The app creates a Firebase user w/ the same info.
With the launcher activity (LoginActivity), if it detects an user is already logged in, it redirects them to their profile fragment.
However, on the profile fragment, I click the log off button and redirect
them to the Login page. This is where the cycle begins.
From code snippets and official Firebase doc, I am using .unauth();. However, my logcat still shows the user is logged in. I've also tried using the Facebook SDK method of LoginManager, but that hasn't worked either.
LogOut method (in MainFrag)
public void LogOut(){
Log.d(TAG, "CHECK #2: USER SIGNED OUT");
getActivity().getIntent().removeExtra("user_id");
getActivity().getIntent().removeExtra("profile_picture");
mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
Log.d(TAG, "signed out" + mUser.getUid());
// mAuth.unauth();
Intent intent = new Intent(getActivity().getApplicationContext(),
LoginActivity.class);
myFirebaseRef.unauth();
LoginManager.getInstance().logOut();
startActivity(intent);
Log.d(TAG, "CHECK 3: FINISIHED SIGNED OUT");
Log.d(TAG, "onAuthStateChanged:signed_out");
}
LoginActivity
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "AndroidBash";
public User user;
private EditText email;
private EditText password;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private ProgressDialog mProgressDialog;
private DatabaseReference mDatabase;
//Add YOUR Firebase Reference URL instead of the following URL
private Firebase mRef=new Firebase("https://fitmaker-ee2c0.firebaseio.com/");
//FaceBook callbackManager
private CallbackManager callbackManager;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDatabase = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Log.d(TAG, "CHECK - FB SIGN IN - USER IS LOGGED IN " + mUser.getUid());
Intent intent = new Intent(getApplicationContext(), CustomColorActivity.class);
String uid = mAuth.getCurrentUser().getUid();
String image=mAuth.getCurrentUser().getPhotoUrl().toString();
intent.putExtra("user_id", uid);
if(image!=null || image!=""){
intent.putExtra("profile_picture",image);
}
startActivity(intent);
finish();
Log.d(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
}
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mUser = firebaseAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + mUser.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
//FaceBook
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
signInWithFacebook(loginResult.getAccessToken());
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
//
}
#Override
protected void onStart() {
super.onStart();
email = (EditText) findViewById(R.id.edit_text_email_id);
password = (EditText) findViewById(R.id.edit_text_password);
mAuth.addAuthStateListener(mAuthListener);
}
#Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
//FaceBook
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
//
protected void setUpUser() {
user = new User();
user.setEmail(email.getText().toString());
user.setPassword(password.getText().toString());
}
public void onSignUpClicked(View view) {
Intent intent = new Intent(this, SignUpActivity.class);
startActivity(intent);
}
public void onLoginClicked(View view) {
setUpUser();
signIn(email.getText().toString(), password.getText().toString());
}
private void signIn(String email, String password) {
Log.d(TAG, "signIn:" + email);
if (!validateForm()) {
return;
}
showProgressDialog();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithEmail:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithEmail", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
String uid = mAuth.getCurrentUser().getUid();
intent.putExtra("user_id", uid);
startActivity(intent);
finish();
}
hideProgressDialog();
}
});
//
}
private boolean validateForm() {
boolean valid = true;
String userEmail = email.getText().toString();
if (TextUtils.isEmpty(userEmail)) {
email.setError("Required.");
valid = false;
} else {
email.setError(null);
}
String userPassword = password.getText().toString();
if (TextUtils.isEmpty(userPassword)) {
password.setError("Required.");
valid = false;
} else {
password.setError(null);
}
return valid;
}
private void signInWithFacebook(AccessToken token) {
Log.d(TAG, "signInWithFacebook:" + token.getToken());
showProgressDialog();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}else{
String uid=task.getResult().getUser().getUid();
String name=task.getResult().getUser().getDisplayName();
String email=task.getResult().getUser().getEmail();
String image=task.getResult().getUser().getPhotoUrl().toString();
//Create a new User and Save it in Firebase database
User user = new User(uid,name,null,email,name);
user = new User();
// user.setId(authData.getUid());
user.setName(name);
user.setEmail(email);
user.saveUser();
// mRef.child("uid").setValue(uid);
// mRef.child(name).setValue(user);
Log.d(TAG, uid);
Intent intent = new Intent(getApplicationContext(), CustomColorActivity.class);
intent.putExtra("user_id",uid);
intent.putExtra("profile_picture",image);
startActivity(intent);
finish();
}
hideProgressDialog();
}
});
}
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
MainFragment.java
public class MainFragment extends Fragment {
private static final String TAG = "AndroidBash";
public User user;
private Firebase myFirebaseRef =new Firebase("https://fitmaker-ee2c0.firebaseio.com/");
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private TextView name;
private TextView welcomeText;
private Button changeButton;
private Button revertButton;
private Button FBButton;
private ProgressDialog mProgressDialog;
// To hold Facebook profile picture
private ImageView profilePicture;
public MainFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
//((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
//Add YOUR Firebase Reference URL instead of the following URL
myFirebaseRef = new Firebase("https://fitmaker-ee2c0.firebaseio.com/");
mAuth = FirebaseAuth.getInstance();
name = (TextView) view.findViewById(R.id.text_view_name);
welcomeText = (TextView) view.findViewById(R.id.text_view_welcome);
changeButton = (Button) view.findViewById(R.id.button_change);
revertButton = (Button) view.findViewById(R.id.button_revert);
FBButton = (Button) view.findViewById(R.id.button_fb);
profilePicture = (ImageView) view.findViewById(R.id.profile_picture);
//Get the uid for the currently logged in User from intent data passed to this activity
String uid = getActivity().getIntent().getExtras().getString("user_id");
//Get the imageUrl for the currently logged in User from intent data passed to this activity
String imageUrl = getActivity().getIntent().getExtras().getString("profile_picture");
Log.d(TAG, "MainFrag - OnCreateView Check");
new ImageLoadTask(imageUrl, profilePicture).execute();
//Referring to the name of the User who has logged in currently and adding a valueChangeListener
myFirebaseRef.child(uid).child("name").addValueEventListener(new ValueEventListener() {
//onDataChange is called every time the name of the User changes in your Firebase Database
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Inside onDataChange we can get the data as an Object from the dataSnapshot
//getValue returns an Object. We can specify the type by passing the type expected as a parameter
String data = dataSnapshot.getValue(String.class);
name.setText("Hello " + data + ", ");
}
//onCancelled is called in case of any error
#Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getActivity().getApplicationContext(), "" + firebaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
//A firebase reference to the welcomeText can be created in following ways :
// You can use this :
//Firebase myAnotherFirebaseRefForWelcomeText=new Firebase("https://androidbashfirebaseupdat-bd094.firebaseio.com/welcomeText");*/
//OR as shown below
myFirebaseRef.child("welcomeText").addValueEventListener(new ValueEventListener() {
//onDataChange is called every time the data changes in your Firebase Database
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Inside onDataChange we can get the data as an Object from the dataSnapshot
//getValue returns an Object. We can specify the type by passing the type expected as a parameter
String data = dataSnapshot.getValue(String.class);
welcomeText.setText(data);
}
//onCancelled is called in case of any error
#Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getActivity().getApplicationContext(), "" + firebaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
//onClicking changeButton the value of the welcomeText in the Firebase database gets changed
changeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myFirebaseRef.child("welcomeText").setValue("Android App Development # AndroidBash");
}
});
FBButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// log user out
// add choice dialog later
LogOut();
}
});
//onClicking revertButton the value of the welcomeText in the Firebase database gets changed
revertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myFirebaseRef.child("welcomeText").setValue("Welcome to Learning # AndroidBash");
}
});
return view;
}
#Override
public void onResume() {
Log.d(TAG, "onResume of MainFragment");
CheckIfLoggedOut();;
super.onResume();
}
public void CheckIfLoggedOut() {
// here, check if user still logged in.
FirebaseUser mUser = mAuth.getCurrentUser();
if (mUser != null) {
// User is signed in
Log.d(TAG, "MainFrag - Signed In (onResume)");
} else {
// User is signed out
Log.d(TAG, "check resume: starting log out for " + mUser.getUid());
LogOut();
}
}
public void LogOut(){
Log.d(TAG, "CHECK #2: USER SIGNED OUT");
getActivity().getIntent().removeExtra("user_id");
getActivity().getIntent().removeExtra("profile_picture");
mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
Log.d(TAG, "signed out" + mUser.getUid());
// mAuth.unauth();
Intent intent = new Intent(getActivity().getApplicationContext(),
LoginActivity.class);
myFirebaseRef.unauth();
LoginManager.getInstance().logOut();
startActivity(intent);
Log.d(TAG, "CHECK 3: FINISIHED SIGNED OUT");
Log.d(TAG, "onAuthStateChanged:signed_out");
}
public void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(getContext());
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
FirebaseAuth.getInstance().signOut();
Adding this piece of line solved my problem.

Complications with ParseFacebookUtils, reading permissions

i'm tryng to integrate facebook login in my android application. For now i get a new row in Parse Backend about the logged user which contains his real name, some weird value(ex:SnvvKEsIv6tX7...) in his userName, and an authData which is Json and contains the following values:
access_token, expiration date, id.
After debugging i got the url of the json response, it only contains two values, the name and the id, however i declared the list like this:
List permissions = Arrays.asList("public_profile", "email");
So for now the graph is only returnin two values, i thought it was due to the permissions but as i sayed i did declared. Here is my code:
public class LoginActivity extends Activity {
private Dialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FacebookSdk.sdkInitialize(getApplicationContext());
ParseFacebookUtils.initialize(this);
// Check if there is a currently logged in user
// and it's linked to a Facebook account.
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
// Go to the user info activity
showUserDetailsActivity();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
public void onLoginClick(View v) {
progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
List<String> permissions = Arrays.asList("public_profile", "email");
// NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
// (https://developers.facebook.com/docs/facebook-login/permissions/)
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
progressDialog.dismiss();
if (user == null) {
Log.d("TAG", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject fbUser,
GraphResponse response) {
/*
* If we were able to successfully retrieve the
* Facebook user's name, let's set it on the
* fullName field.
*/
Log.e("facebook User", fbUser.toString());
final ParseUser parseUser = ParseUser
.getCurrentUser();
if (fbUser != null
&& parseUser != null
&& fbUser.optString("name").length() > 0) {
parseUser.put("name",
fbUser.optString("name"));
parseUser.put("email",
fbUser.optString("email"));
Log.v("isnew, values: ","values : "+fbUser.optString("name")+","+fbUser.optString("email")+","+fbUser.optString("age_range")+","+fbUser.optString("locale"));
parseUser
.saveInBackground(new SaveCallback() {
#Override
public void done(
ParseException e) {
if (e != null) {
Log.v("", (getString(R.string.com_parse_ui_login_warning_facebook_login_user_update_failed)
+ e.toString()));
}
ParseInstallation installation = ParseInstallation
.getCurrentInstallation();
installation
.put(ParseConstants.KEY_USER_ID,
parseUser
.getUsername());
installation
.saveInBackground();
showUserDetailsActivity();
}
});
}
}
}).executeAsync();
} else {
Log.d("TAG", "User not new logged in through Facebook!");
showUserDetailsActivity();
}
}
});
}
private void showUserDetailsActivity() {
Intent intent = new Intent(this, UserDetailsActivity.class);
startActivity(intent);
}
}
public class UserDetailsActivity extends Activity {
private ProfilePictureView userProfilePictureView;
private TextView userNameView;
private TextView userGenderView;
private TextView userEmailView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userdetails);
userProfilePictureView = (ProfilePictureView) findViewById(R.id.userProfilePicture);
userNameView = (TextView) findViewById(R.id.userName);
userGenderView = (TextView) findViewById(R.id.userGender);
userEmailView = (TextView) findViewById(R.id.userEmail);
//Fetch Facebook user info if it is logged
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && currentUser.isAuthenticated()) {
makeMeRequest();
}
}
#Override
public void onResume() {
super.onResume();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
// Check if the user is currently logged
// and show any cached content
updateViewsWithProfileInfo();
} else {
// If the user is not logged in, go to the
// activity showing the login view.
startLoginActivity();
}
}
private void makeMeRequest() {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject != null) {
JSONObject userProfile = new JSONObject();
try {
userProfile.put("facebookId", jsonObject.getLong("id"));
userProfile.put("name", jsonObject.getString("name"));
if (jsonObject.getString("gender") != null)
userProfile.put("gender", jsonObject.getString("gender"));
if (jsonObject.getString("email") != null)
userProfile.put("email", jsonObject.getString("email"));
// Save the user profile info in a user property
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("profile", userProfile);
currentUser.saveInBackground();
// Show the user info
updateViewsWithProfileInfo();
} catch (JSONException e) {
Log.d("TAG",
"Error parsing returned user data. " + e);
}
} else if (graphResponse.getError() != null) {
switch (graphResponse.getError().getCategory()) {
case LOGIN_RECOVERABLE:
Log.d("TAG",
"Authentication error: " + graphResponse.getError());
break;
case TRANSIENT:
Log.d("TAG",
"Transient error. Try again. " + graphResponse.getError());
break;
case OTHER:
Log.d("TAG",
"Some other error: " + graphResponse.getError());
break;
}
}
}
});
request.executeAsync();
}
private void updateViewsWithProfileInfo() {
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser.has("profile")) {
JSONObject userProfile = currentUser.getJSONObject("profile");
try {
if (userProfile.has("facebookId")) {
userProfilePictureView.setProfileId(userProfile.getString("facebookId"));
} else {
// Show the default, blank user profile picture
userProfilePictureView.setProfileId(null);
}
if (userProfile.has("name")) {
userNameView.setText(userProfile.getString("name"));
} else {
userNameView.setText("");
}
if (userProfile.has("gender")) {
userGenderView.setText(userProfile.getString("gender"));
} else {
userGenderView.setText("");
}
if (userProfile.has("email")) {
userEmailView.setText(userProfile.getString("email"));
} else {
userEmailView.setText("");
}
} catch (JSONException e) {
Log.d("TAG", "Error parsing saved user data.");
}
}
}
public void onLogoutClick(View v) {
logout();
}
private void logout() {
// Log the user out
ParseUser.logOut();
// Go to the login view
startLoginActivity();
}
private void startLoginActivity() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
i'm using Parse 1.9.4 and ParseFacebookUtils 1.9.4.

Categories

Resources