How to logout ParseUser linked with Twitter in android - 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.

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.

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.

Check existing user before Facebook/Twitter signup Parse Android SDK

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.

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.

Firebase: How to keep an Android user logged in?

I'm using Firebase SimpleLogin to enable Email / Password authentication. Creation of users and subsequent login is all working fine. However, whenever I leave the app (even if only for a few seconds) the user is never logged in on my return i.e...
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler())...
Always returns a null user.
I am not logging out the user via the API. Also I have set the number of days the user is logged in to 21 in the Firebase console.
I have seen mention of a remember-me param in the JS docs, but I can't see any equivalent for Android / Java.
Wondering if I'm missing anything in the docs or if it's not possible for Android?
Thanks for your help,
Neil.
Edit: Added code sample.
User creation....
public void registerUserForChat(final MyApplication application, String email, String password) {
Firebase ref = new Firebase(FIREBASE_URL);
SimpleLogin authClient = new SimpleLogin(ref);
authClient.createUser(email, password, new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
Log.e(TAG, "Error attempting to create new Firebase User: " + error);
}
else {
Log.d(TAG, "User successfully registered for Firebase");
application.setLoggedIntoChat(true);
}
}
});
}
User login....
public void loginUserForChat(final MyApplication application, String email, String password) {
Log.d(TAG, "Attempting to login Firebase user...");
Firebase ref = new Firebase(FirebaseService.FIREBASE_URL);
final SimpleLogin authClient = new SimpleLogin(ref);
authClient.checkAuthStatus(new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if (error != null) {
Log.d(TAG, "error performing check: " + error);
} else if (user == null) {
Log.d(TAG, "no user logged in. Will login...");
authClient.loginWithEmail(email, password, new SimpleLoginAuthenticatedHandler() {
#Override
public void authenticated(com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
if(com.firebase.simplelogin.enums.Error.UserDoesNotExist == error) {
Log.e(TAG, "UserDoesNotExist!");
} else {
Log.e(TAG, "Error attempting to login Firebase User: " + error);
}
}
else {
Log.d(TAG, "User successfully logged into Firebase");
application.setLoggedIntoChat(true);
}
}
});
} else {
Log.d(TAG, "user is logged in");
}
}
});
}
So loginUserForChat method first checks to see if there is a logged in user and, if not, performs the login. Note that every time I start the app, the logging I see is....
Attempting to login Firebase user...
no user logged in. Will login...
User successfully logged into Firebase
If I exit the app, even for a few seconds, and return - I see the same logging.
One thing I noticed is that the call to checkAuthStatus does not take any user credentials - I assume it just checks for any locally logged in user?
Much appreciated.
Another way - try this code in your onCreate:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
// User is signed in
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
This will keep the user logged in by taking the user to the Main activity directly without stopping at registration activity. so the user will be logged in unless the user click on signout.
[Engineer at Firebase] In order to transparently handle persistent sessions in the Firebase Simple Login Java client, you need to use the two-argument constructor which accepts an Android context, i.e. SimpleLogin(com.firebase.client.Firebase ref, android.content.Context context) every time you instantiate the Simple Login Java client.
See https://www.firebase.com/docs/java-simple-login-api/javadoc/com/firebase/simplelogin/SimpleLogin.html for the full API reference.
The proper way to do it is to use oAuth authentication:
1. The user logs in.
2. You generate an access token(oAuth2).
3. Android app saves the token locally.
4. Each time the comes back to the auth, he can use the token to to log in, unless the token has been revoked by you, or he changed his
password.
Luckily, firebase has an out of the box support for that, docs:
https://www.firebase.com/docs/security/custom-login.html
https://www.firebase.com/docs/security/authentication.html
You can do this by Using this Approach to escape logi page if User already logged in.
private FirebaseAuth auth;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
auth = FirebaseAuth.getInstance();
if (auth.getCurrentUser() != null) {
startActivity(new Intent(Login_Activity.this, Home.class));
finish();
}
setContentView(R.layout.activity_login_);
for those using Kotlin, to keep the user logged in just add in the onCreate function
if (auth.currentUser != null)
{
startActivity(Intent(this#Login, SellingPageHolderActivity::class.java))
finish()
}

Categories

Resources