How to change password using old password on parse android - android

I have an android app in which user can change his/her password my problem is how i can verify old password of user using parse i have 3 edit text "old password, new password and confirm new password".
I search on parse.com but can't find any solution parse do not fetch data using get password.
i am doing this
String get_confrimpass=currentuser.getpassword();
if(get_confrimpass.replaceAll("\\s", "").equals(current_pass_check))
{ }

You can try logging them in using there current username and the password that they have given to you. If the loggin is successful the old password is correct. I.e
ParseUser.logInInBackground(ParseUser.getCurrentUser().getUsername(), currentPassword, new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The password is correct
} else {
// The password was incorrect
}
}
});
In the example above the 'currentPassword' variable is the text that you would retreive from the 'Old Password' EditText

final ParseUser currentUser = ParseUser.getCurrentUser();
final String userName = ParseUser.getCurrentUser().getUsername();
ParseUser.logInInBackground(userName, oldPass, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (user != null) {
if (et.length() < 6)
Toast(getActivity(), "Password is short, Min char 6", Toast.LENGTH_LONG).show();
else {
currentUser.setPassword(newPass);
currentUser.saveInBackground();
ParseUser.logOut();
ParseUser.logInInBackground(userName, newPass, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null) {
Toast(getActivity(), "Password change", Toast.LENGTH_LONG).show();
} else
Toast(getActivity(), "Network Error", Toast.LENGTH_LONG).show();
}
});
}
} else {
new CustomToast(getActivity(), "Old Password is incorrect", Toast.LENGTH_LONG);
}
}
});

Related

Parse Facebook unable get user email and gender

Currently, I am using this tutorial to create the facebook login.
"https://github.com/ParsePlatform/ParseUI-Android"
The Parse user JSON didn't return me an email or gender. I also have research around but I still can't find the solution (well, at least, some other still returning null). Anyone can explain the reason and how to solve it?
facebookLoginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
loadingStart(false); // Facebook login pop-up already has a spinner
if (config.isFacebookLoginNeedPublishPermissions()) {
ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
Arrays.asList(
"public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser","Publish");
} else {
ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
Arrays.asList( "public_profile", "email"), facebookLoginCallbackV4);
Log.i("fbUser", "Read");
}
}
});
private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if (isActivityDestroyed()) {
return;
}
if (user == null) {
loadingFinish();
if (e != null) {
showToast(R.string.com_parse_ui_facebook_login_failed_toast);
debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
e.toString());
}
} 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.
*/
ParseUser parseUser = ParseUser.getCurrentUser();
if (fbUser != null && parseUser != null && fbUser.optString("name").length() > 0) {
Log.i("FbUSer", response.getJSONObject().toString());
Log.d("FbUSer-Email",fbUser.optString("email"));
Log.i("FbUSer-gender",fbUser.optString("gender"));
parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
debugLog(getString(
R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
e.toString());
}
loginSuccess();
}
});
}
loginSuccess();
}
}
).executeAsync();
} else {
loginSuccess();
}
}
};
Logcat Section return:
{"id":"1234524124124","name":"FbUser NAme"}
I just found the answer. I just need to set the parameter so I can get the data I want.
GraphRequest request = GraphRequest.newMeRequest(...);
Bundle parameters = new Bundle();
parameters.putString("fields","id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
Reference: https://stackoverflow.com/a/32579366/4961962
for more user info you have to add permissoin to access those detail like email, user_about_me at facebook developer console
select app at facebook developer and go -> app details -> Configure
App Center Permissions

Fetch twitter data in android using parse.com

Need to fetch twitter user profile data after successfully logging in using parse. Please refer below code :
ParseTwitterUtils.logIn(SignupActivity.this, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (parseUser == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (parseUser.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
} else {
Log.d("MyApp", "User logged in through Twitter!");
}
}
});
I tried to get values from Parseuser object returned after login but it is showing null.
Suggest what should I do after logging in.
Thanks
First off: Are you sure that you are logged in? To verify this, make sure that in your console there is the Log "User logged in through Twitter!", if so, you can add:
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
under your else if and else block, or you can replace your code with this:
ParseTwitterUtils.logIn(SignupActivity.this, new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
if (parseUser == null) {
Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
} else if (parseUser.isNew()) {
Log.d("MyApp", "User signed up and logged in through Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
} else {
Log.d("MyApp", "User logged in through Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
}
}
});
If your class is not "MainActivity", type it in the Log.d.
If you are having trouble singing in, you can also try this:
user = new ParseUser();
user.setUsername("Username");
user.setPassword("password");
user.setEmail("email#example.com");
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
}
}
});
if (!ParseTwitterUtils.isLinked(user)) {
ParseTwitterUtils.link(user, this, new SaveCallback() {
#Override
public void done(ParseException ex) {
if (ParseTwitterUtils.isLinked(user)) {
Log.d("MyApp", "Woohoo, user logged in with Twitter!");
String twitter = ParseTwitterUtils.getTwitter().getScreenName();
Log.d(MainActivity.class.getSimpleName(), twitter + "");
}
}
});
}
define "ParseUser user;" outside the onCreate method.

Change Password and Forgot Password with ParseUser in Parse.com

I am working on an application using Parse.com as backend support.I have used ParseUser for login and signup ,but now I have to implement change Password and forgot password ,but don't know how to implement it..Please help me to implement this functionalities.
The code which I have used to login the ParseUser is as follows:
ParseUser.logInInBackground(str_email2, str_password2, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
dlg.dismiss();
if(e == null)
{
Log.d(">>>","ObjId>>"+user.getObjectId()+" Username>>>"+user.getUsername());
}
else
loginUnSuccessful();
}
});
To request a new password (in case the user forgot it) you can use this:
ParseUser.requestPasswordResetInBackground("myemail#example.com",
new RequestPasswordResetCallback() {
public void done(ParseException e) {
if (e == null) {
// An email was successfully sent with reset instructions.
} else {
// Something went wrong. Look at the ParseException to see what's up.
}
}
});
And to change a password you can just get the current user and do the following:
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.setPassword("new_password");
currentUser.saveInBackground();
In the doc, you have this sample code:
ParseUser.requestPasswordResetInBackground("myemail#example.com",
new RequestPasswordResetCallback() {
public void done(ParseException e) {
if (e == null) {
// An email was successfully sent with reset instructions.
} else {
// Something went wrong. Look at the ParseException to see what's up.
}
}
});
Source: https://parse.com/docs/android_guide#users-resetting
You can not reset the password for not signed (anonymous) ParseUser.
If you are using email feature for ParseUser, you can send password reset mail like this
ParseUser.requestPasswordResetInBackground(
stringEmail,
new RequestPasswordResetCallback() {
public void done(ParseException e) {
...
}
}
);
If you are not using email feature, you need to write a cloud method in your main.js file, like this
Parse.Cloud.define("resetPassword", function (request, response) {
Parse.Cloud.useMasterKey(); /*important*/
var username = request.params.username;
var password = request.params.password;
var objUser = Parse.Object.extend("User");
var qUser = new Parse.Query(objUser);
qUser.equalTo("username", username);
qUser.find().then(function (resultObj) {
if(resultObj.length > 0){
var userObj = resultObj[0];
userObj.set("password", password);
return userObj.save();
} else {
response.error("User does not exist");
}
}).then(function () {
response.success("Password RESET DONE SUCCESSFULLY");
}, function (error) {
response.error("Error " + error);
});
});
and call that method from your app, like this
Map<String, Object> params = new HashMap<String, Object>();
params.put("password", password);
params.put("username", username);
ParseCloud.callFunctionInBackground("resetPassword", params, new FunctionCallback<String>() {
#Override
public void done(String response, ParseException e) {
if (e == null) {
...
} else {
..
}
}
});
This way you can reset user password using username or mobile number.

Converting an anonymous user to a regular user and saving

I am writing a Parse Android application which uses anonymous users by enabling automatic user creation. I can successfully signup this user (to convert the anonymous user into a regular user) and after that, logging in. Both calls return successfully. When I then set data on the user object and trying to save the user, I get a stackoverflow in the ParseObject class.
My code looks like this (simplified example code):
user.setUsername("MyUserName");
final String password = new PasswordGenerator().nextSessionId();
user.setPassword(password);
try {
user.signUp();
ParseUser.logInInBackground(verification.getPhoneNumber(), password, new LogInCallback() {
#Override
public void done(final ParseUser parseUser, final ParseException e) {
if (parseUser == null) {
requestListener.onError(e);
} else {
parseUser.put("phone_no", "123");
parseUser.saveInBackground(new SaveCallback() { // This generates a stackoverflow
#Override
public void done(final ParseException e) {
int i = 0;
System.out.println("i = " + i);
}
});
// requestListener.onSuccess(null);
}
}
});
} catch (ParseException e) {
e.printStackTrace();
}
The stack overflow is generated when calling saveInBackground on the ParseUser. The stackoverflow looks like this:
java.lang.StackOverflowError
at com.parse.ParseObject.isDataAvailable(ParseObject.java:3212)
at com.parse.ParseObject.checkGetAccess(ParseObject.java:3284)
at com.parse.ParseObject.getString(ParseObject.java:2867)
at com.parse.ParseUser.getUsername(ParseUser.java:401)
at com.parse.ParseUser.signUpAsync(ParseUser.java:665)
at com.parse.ParseUser.resolveLazinessAsync(ParseUser.java:1397)
at com.parse.ParseUser.saveAsync(ParseUser.java:502)
at com.parse.ParseUser.signUpAsync(ParseUser.java:681)
at com.parse.ParseUser.resolveLazinessAsync(ParseUser.java:1397)
at com.parse.ParseUser.saveAsync(ParseUser.java:502)
at com.parse.ParseUser.signUpAsync(ParseUser.java:681)
The Parse version I use is 1.8.0.
Any ideas?
I found the solution! Turns out you have to save the anonymous user first before converting the user into a registered one. If you save the user first, convert the user into a registered one and then do a saveInBackground, you won't get a stackoverflow. So the full code to convert an anonymous user is:
final String accountUsername = username.getText().toString();
final String accountPassword = password.getText().toString();
final ParseUser user = ParseUser.getCurrentUser();
user.setUsername(accountUsername);
user.setPassword(accountPassword);
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(final ParseException e) {
if (e != null) {
Toast.makeText(MainActivity.this, "Signup Fail", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} else {
Toast.makeText(MainActivity.this, "Signup success", Toast.LENGTH_SHORT).show();
final ParseUser user = ParseUser.getCurrentUser();
user.put("phone_no", "31743379507");
user.saveInBackground(new SaveCallback() {
#Override
public void done(final ParseException e) {
if (e != null) {
Toast.makeText(MainActivity.this, "Save data Fail", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} else {
Toast.makeText(MainActivity.this, "Save data success", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
Please note that the saveInBackground in the SignupCallback is optional. You could also set this data on the user before the signUpInBackground is called and save an extra roundtrip. This is pure for demonstration purposes.
Also, it is assumed the following code is placed in the Application class to allow anonymous users:
ParseUser.enableAutomaticUser();
ParseUser.getCurrentUser().saveInBackground();
Here you see the user is saved as soon it is created.
findViewById(R.id.createUser).setOnClickListener(newView.OnClickListener() {
#Override
public void onClick(final View v) {
final String accountUsername = username.getText().toString();
final String accountPassword = password.getText().toString();
final ParseUser user = ParseUser.getCurrentUser();
user.setUsername(accountUsername);
user.setPassword(accountPassword);
user.signUpInBackground(new SignUpCallback() {
#Override
public void done(final ParseException e) {
if (e != null) {
Toast.makeText(MainActivity.this, "Signup Fail",
Toast.LENGTH_SHORT).show();
Log.e(TAG, "Signup fail", e);
} else
{ Toast.makeText(MainActivity.this,"Signupsuccess",Toast.LENGTH_SHORT).show();
final ParseUser user = ParseUser.getCurrentUser();
user.put("phone_no", "31612345678");
user.saveInBackground(new SaveCallback() {
#Override
public void done(final ParseException e) {
if (e != null) {
Toast.makeText(MainActivity.this, "Save data Fail", Toast.LENGTH_SHORT).show();
Log.e(TAG, "Signup fail", e);
} else {
Toast.makeText(MainActivity.this, "Save
data success", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
})

ParseUser setPassword() isn't saving the password

I want a unique user but I don't need it to be a formal thing for my app. So, on launch I inspect shared preferences for a previously stored username. I handle the user creation or login like so:
String parseUsername = _appPrefs.getParseUsername();
_progress.setVisibility(View.VISIBLE);
if (parseUsername == null) {
Log.v(TAG, "Creating a user.");
ParseUser.enableAutomaticUser();
_user = ParseUser.getCurrentUser();
_user.setPassword("abc123");
_user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.w(TAG, String.format("User '%s' created.", _user.getUsername()));
_progress.setVisibility(View.GONE);
_appPrefs.saveParseUsername(_user.getUsername());
createStory();
return;
}
Log.e(TAG, "Error creating user: ", e);
}
});
} else {
Log.v(TAG, String.format("Logging the user '%s' in with password '%s'.", parseUsername, "abc123"));
ParseUser.logInInBackground(parseUsername, "abc123", new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
Log.v(TAG, "User logged in.");
_progress.setVisibility(View.GONE);
if (e == null) {
createStory();
return;
}
Log.e(TAG, "Error logging in: ", e);
}
});
}
I can see the user being successfully created in the logs. I can kill the app and re-launch but it always fails with "invalid login credentials" response from Parse.
If I manually enter abc123 in the Parse data browser then everything works. Ideas?

Categories

Resources