After troubleshooting, I managed to resolve the issue by identifying and handling Parse errors using the following code snippet:
function handleParseError(err) {
switch (err.code) {
case Parse.Error.INVALID_SESSION_TOKEN:
var currentUser = Parse.User.current();
var query = new Parse.Query(Parse.User);
var username = currentUser.get("username");
var codeEntry;
query.equalTo("username", username);
query.first().then(function(result) {
codeEntry = result.get("codeEntry");
});
Parse.User.logOut();
Parse.Cloud.run("logIn", {"username": username, "codeEntry": codeEntry});
break;
}
}
In essence, in the event of an invalid session token, I retrieve the username
along with the codeEntry
(this application is designed for AnyPhone). Subsequently, I perform a user logout using Parse.User.logOut()
followed by executing a Cloud Code function named logIn
using Parse.Cloud.run()
:
Parse.Cloud.define("logIn", function(request, response) {
Parse.Cloud.useMasterKey();
var phoneNumber = request.params.phoneNumber;
phoneNumber = phoneNumber.replace(/\D/g, '');
if (phoneNumber && request.params.codeEntry) {
Parse.User.logIn(phoneNumber, request.params.codeEntry).then(function (user) {
response.success(user.getSessionToken());
}, function (error) {
response.error(error);
});
} else {
response.error('Invalid parameters.');
}
});
With these implementations, everything now functions seamlessly!