Check existing user before Facebook/Twitter signup Parse Android SDK - android

I'm working with Parse Android SDK and I have two signup options in my signup activity. Signup by twitter and Signup by facebook.
Here's the problem:
If I signup using facebook and then logout and then again signup using twitter, instead of linking the new twitter parse user to the old facebook parse user, it creates a new user even though the email used in both facebook and twitter were same.
What I want to achieve is to check if the user with the same email exists if not then create new user and if he does exist then link the signup method (facebook or twitter) with the existing parse user.
Any help would be greatly appreciated.
Thanks
private void loginWithTwitter() {
signUpProgressDialog.show();
ParseTwitterUtils.logIn(this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
signUpProgressDialog.dismiss();
if(e==null) {
if (user == null) {
Log.d(TAG, "Uh oh. The user cancelled the Twitter login.");
} else if (user.isNew()) {
Log.d(TAG, "User signed up and logged in through Twitter!");
startAppropriateActivity("TWITTER_NEW");
} else {
Log.d(TAG, "User logged in through Twitter!");
startAppropriateActivity("TWITTER_OLD");
}
}else Log.d(TAG,"Twitter Parse Error");
}
});
}
private void loginWithFacebook() {
signUpProgressDialog.show();
List<String> permissions = Arrays.asList("public_profile", "email");
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
signUpProgressDialog.dismiss();
if(e==null) {
if (user == null) {
Log.d(TAG, "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(TAG, "User signed up and logged in through Facebook!");
startAppropriateActivity("FACEBOOK_NEW");
} else {
Log.d(TAG, "User logged in through Facebook!");
startAppropriateActivity("FACEBOOK_OLD");
}
}else Log.d(TAG,"Facebook Parse Error");
}
});
}

You can set user's email as a content of username field in the ParseUser object. So next time user will try to signup with same username you will receive ParseException with code 202 - "User with these username and password already registered" and in that case you can call login() method.
But in this case password should stay the same between two sessions.

Related

ParseFacebookUtils - How to Open Facebook Login Dialog every time?

My question is same as this thread but the answer in it does not work anymore because ParseFacebookUtils.getSession() has been deprecated.
What is the replacement on how to log out and flush the cache so that next time the Facebook login button appears again.
Just doing ParseUser.logOut() did not help.
Here is my code to login:
List<String> permissions = Arrays.asList("public_profile", "email");
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, permissions, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
Log.v("debug", "Did enter the done for LoginCallBack.");
if (user == null) {
Log.v("debug", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.v("debug", "User signed up and logged in through Facebook!");
} else {
Log.v("debug", "User logged in through Facebook!");
}
}
});
I have tried to logout with the code below, but it still does not help.
//Now call logout
ParseUser.logOutInBackground(new LogOutCallback() {
#Override
public void done(ParseException e) {
Log.v("debug", "in LogOutCallback() - done() - i guess successfully done?");
}
});
I haven't find a way to do that so i think that there is no way to do that if the user has the FB app installed.
I think it works like this:
If the user doesn't have the FB app then the login is always shown.
If the user has the FB app, then the first time is asked to login (or permissions ,I don't remember), then the login doesn't appear anymore unless the user logouts from FB app.

android parse sdk facebook login cannot get email

Im integrated parse and facebook sdk to my project and its work and i get id and user name of facebook user
my issue is i cannot get any thing else like email or gender or any other information even i add the permission for them.
this is permission :
private static final List<String> Permissions = Arrays.asList("public_profile","email","user_friends","user_birthday","user_location","user_relationships");
button click code :
Button mBtnFb=(Button)findViewById(R.id.mBtnFb);
mBtnFb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseFacebookUtils.logInWithReadPermissionsInBackground(MainActivity.this,Permissions , new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d("MyApp", "The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("MyApp", "User signed up and logged in through Facebook!");
getUserDetailsFromFB();
} else {
Log.d("MyApp", "User logged in through Facebook!");
getUserDetailsFromParse();
}
}
});
}
});
get user details from facebook:
public String name,email;
ParseUser parseUser=new ParseUser();
private void getUserDetailsFromFB() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
try {
String id=object.getString("id");
String name=object.getString("name");
String email=object.getString("email");
String gender=object.getString("gender");
String birthday=object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email");
request.setParameters(parameters);
request.executeAsync();
}
I get id and name only and any other info give me exception any suggestion ?
I had exactly the same issue, here's how I solved it:
Go to https://developers.facebook.com/
Click "Tools & Support" (at top menu)
Click "Graph API Explorer" (at left side menu called "Tools")
Where it says "Application: Graph API Explorer" (at the top right) change it to the application you are working with.
Where it says "Get Token" click "Get User Access Token"
Tick "user_about_me"
Click on "Extended Permissions"
Tick "email"
Click "Get Access Token"
Facebook may prompt you to confirm.
This should then give your application the permission to retrieve the email address of the facebook user.
Hope this helps.
Regards,
Nathanial

Facebook asking for login again Android

this is my code which uses Parse to log a user in via Facebook.
public void onFacebookLoginClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
if ((currentUser != null) && ParseFacebookUtils.isLinked(currentUser)) {
Log.d("onFacebookLoginClick", "Already signed in");
showHomeActivity();
} else {
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("Facebook", "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d("onFacebookLoginClick: ", "User signed up and logged in through Facebook!");
showHomeActivity();
} else {
Log.d("onFacebookLoginClick: ", "User logged in through Facebook!");
showHomeActivity();
}
}
});
}
}
The problem is, the user logs in the first time (OK!), then the login dialog opens up again.
Note: When I close the dialog, it does take me to the next activity. But why can it be showing the login dialog twice?
I had a similar issue implementing this. I solved it by letting my code sleep for a few seconds before attempting to start the activity. It's possible this may be the bug.
--Why I arrived at this conclusion--
I found the loginCallBack in ParseFacebookUtils takes a few seconds to return with its result code/parse user.
No data was returned by the time I start the activity and the activity is configured to launch the login activity if no user is logged in. This made it appear as though the code wasn't logging in on first try. I believe the Parse Facebook logic is a little slower than the regular parse login logic.
The second time you run the code (click the button) the callback was already returned with the parse user and would successfully launch the activity without returning.

Android - Facebook session CLOSED when using ParseFacebookUtils.logIn

I am using Facebook and Parse SDK in my Android app. I followed this tutorial for Facebook login and authentication, changing between fragments for login fragment and main menu fragment depending on whether the session state is OPENED in the Session.StatusCallback. And the app works perfectly before integrating with Parse.
And now I encounter a problem. In the onResume() method of the main menu fragment, I added the following code.
final Session session = Session.getActiveSession();
if(session != null && session.isOpened()) {
Request meRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser graphUser, Response response) {
if(session == Session.getActiveSession()) {
// Check if the session is same as usual
ParseFacebookUtils.logIn(graphUser.getId(), session.getAccessToken(),
session.getExpirationDate(), new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
// The user wasn't saved.
System.out.println("User was not saved to Parse. ");
} else {
// The user has been saved to Parse.
System.out.println("User has successfully been saved to Parse.");
if (user.isNew()) {
// This user was created during this session with Facebook Login.
System.out.println("ParseUser created.");
} else {
// This user existed before.
System.out.println("User exists in Parse. Pull their values: " + user);
}
}
}
});
}
}
});
meRequest.executeAsync();
}
So when the fragment is resumed and the Facebook session is opened, the Facebook user is added to the Parse database so that I can use ParseUser to put and get data afterwards. But the problem happens when using ParseFacebookUtils.logIn(), that it makes the Facebook session CLOSED and invokes the Session.StatusCallback, thus switching the visible fragment back to the login fragment.
I was dealing with this problem all day but cannot find a solution. I have added the code below in an Application but still not work.
Parse.enableLocalDatastore(this);
Parse.initialize(this, Application_id, Client_key);
ParseFacebookUtils.initialize(getString(R.string.facebook_app_id));
Is there a way to fix this? I have read this but no quite good solution is provided. Thanks in advance!
I was trying to do the same thing today. I was using the facebook sdk's UiLifecycleHelper to do the facebook login, make the graph api request and add the user to Parse db. That is how it is done in the example you mentioned - which I think is a bit outdated.
ParseFacebookUtils handles the session and all we need to do is call the login method on it, make the graph api call, get user data like email and update the user field.
Here's some code:
private void parseFacebookLogin(){
ParseFacebookUtils.logIn(this, PARSE_FB_LOGIN_CODE, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
if (user == null) {
Log.d(tag, "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(tag, "User signed up and logged in through Facebook!");
Request.newMeRequest(ParseFacebookUtils.getSession(), graphUserCallback).executeAsync();
} else {
Log.d(tag, "User logged in through Facebook!");
}
}
});
}
GraphUserCallback graphUserCallback = new GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null){
getUserDataFacebook(user);
Log.d(tag, response.toString());
}
}
};
private void getUserDataFacebook(GraphUser user){
//get user data here
}
Check out Parse's docs on Facebook login.
Let me know if this works for you.

How to logout ParseUser linked with Twitter in android

I loggedIn in parse using ParseTwitterUtils:
ParseTwitterUtils.logIn(this, new LogInCallback()
{
#Override
public void done(ParseUser user, ParseException err)
{
if(user == null)
{
Log.d(TAG, "Uh oh. The user cancelled the Twitter login.");
}
else if(user.isNew())
{
Log.d(TAG, "User signed up and logged in through Twitter!");
navigateToMainActivity();
}
else
{
Log.d(TAG, "User logged in through Twitter!");
Log.d(TAG, "user id=" + user.getObjectId());
Log.d("TAG", "username=" + user.getUsername());
navigateToMainActivity();
}
}
});
I logout the user using:
ParseUser.logOut();
However when I try to login again using ParseTwitterUtils, I am presented with webView with only Authorize app button. After pressing Authorize app, I get non-null user in done method but all other fields of the user are null, e.g userId, username, etc. I think app is authorizing the twitter locally and thus not bringing the user from Parse. How can I force Parse to clear twitter session info on logout or how can I get right user in ParseTwitterUtils.login without doing this?
I was experiencing this issue because I have enabled Parse local datastore in application class
Parse.enableLocalDatastore(this);
I just disabled the Parse local datastore and Twitter login is working perfectly.

Categories

Resources