Page 4 of 6

Forgot Password Service

Posted: Thu Dec 05, 2013 6:05 am
by fender

This Node.js (from Sendgrid Docs) does not work as backend/serverside code:

var sendgrid = require('sendgrid')(api_user, api_key);
sendgrid.send({
to: 'a href="mailto:example@example.com" rel="nofollow"example@example.com/a',
from: 'a href="mailto:other@example.com" rel="nofollow"other@example.com/a',
subject: 'Hello World',
text: 'My first email through SendGrid.'
}, function(err, json) {
if (err) { return console.error(err); }
console.log(json);
});


Forgot Password Service

Posted: Thu Dec 05, 2013 6:29 am
by maxkatz

Although the concept is the same, we don't use Node.js for our server code, and plus you probably need to include a library from SendGrid to use that code. You can use the API that I referenced above to invoke standard REST API.


Forgot Password Service

Posted: Thu Dec 05, 2013 6:39 am
by fender

So how do I use standard rest api on the backend/server to do this:

//
//here is your "send E-mail" code. Send E-mail (for example via SendGrid) to 'email' with randomString
//this should help http://docs.appery.io/documentation/b...
//

as quoted from Katya Yakusheva example above?


Forgot Password Service

Posted: Thu Dec 05, 2013 8:55 am
by Maryna Brodina

Hello! SendGrid has API http://sendgrid.com/docs/API_Referenc..., you can call that API from server code http://docs.appery.io/documentation/b.... For example prevar XHRResponse = XHR.send("POST", "https://api.sendgrid.com/api/mail.send.json", { "parameters": {"api_user":"your_sendgrid_username", "api_key":"your_sendgrid_password", "to":"destination@example.com", "toname":"Destination", "subject":"Example_Subject","text":"testingtextbody","from":"info@domain.com"}});/pre


Forgot Password Service

Posted: Thu Dec 05, 2013 1:24 pm
by Doug Black

I'm excited about the talk on here. So here's the beginning of the server code. I'm a Javascript newbie, so please let me know if I'm on the right track.

precode
var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys&#46;length; key++) {
requestParams[paramKeys[key]] = request&#46;get(paramKeys[key]);
}

&#47;&#47; set request parameters
var DB_id = requestParams['X-Appery-Database-Id'];
var email = requestParams['email'];
var randomString = Math&#46;random()&#46;toString(36)&#46;slice(-12);

&#47;&#47; query which user this may refer to
try {
result = {};
var params = {};
params&#46;criteria = {"email": email};
result&#46;query = Collection&#46;query(DB_id, 'Users', params);
console&#46;log(result);
response&#46;success(result, "applications&#47;json");
} catch (e) {
response&#46;success("message: " + e&#46;message + "\ncode: " + e&#46;code);
}

&#47;&#47; write temporary password
var tempUser = JSON&#46;parse(result);
tempUser = JSON && JSON&#46;parse(result) || $&#46;parseJSON(result);

try {
result = {};
iRS= { "randomcode": randomString };
Collection&#46;updateObject(DB_id, 'Users', tempUser&#46;_id, iRS);
response&#46;success(itWork, "applications&#47;json");
} catch (e) {
response&#46;success("message: "+ e&#46;message + "\ncode: " + e&#46;code);
}

&#47;&#47; send email via SendGrip

responseBody&#46;requestBody = request&#46;body();
responseBody&#46;requestParams = requestParams;
console&#46;log(responseBody);
response&#46;success(responseBody, "application&#47;json");
/code/pre


Forgot Password Service

Posted: Thu Dec 05, 2013 2:56 pm
by Maryna Brodina

Hello Doug! In code preCollection&#46;query(DB_id, 'Users', params);/pre what do you mean on 'Users'? Is it standard Users collection or created by you? If it's your collection that's ok. But if it's standard collection you can't access it like that due to security reasons. You have to do that through XMLHttpRequest requests http://docs.appery.io/documentation/b.... Here is an example prevar dbId='xxxxxxxxxx',
masterKey = "xxxxxxx-xxxx-xxxx-xxx-xxxxxxxxx",
mail = request&#46;get("email"),
newPass = request&#46;get("newPass");

&#47;&#47;find user with email from request parameter
var XHRResponse = XHR&#46;send("GET",
"https:&#47;&#47;api&#46;appery&#46;io&#47;rest&#47;1&#47;db&#47;users&#47;",
{ "headers":{"X-Appery-Database-Id": dbId, "X-Appery-Master-Key": masterKey},
"parameters": {"where": encodeURIComponent('{"email": "' + mail + '"}')}
}
);
if (XHRResponse&#46;body&#46;length) { &#47;&#47;if user exists
var userId = XHRResponse&#46;body[0]["_id"];
&#47;&#47;update user info
var XHRResponse = XHR&#46;send("PUT",
"https:&#47;&#47;api&#46;appery&#46;io&#47;rest&#47;1&#47;db&#47;users&#47;" + userId,
{ "headers":{"X-Appery-Database-Id": dbId, "X-Appery-Master-Key": masterKey, "Content-Type":"application&#47;json"},
"body": {"password": newPass}
}
);
response&#46;success(XHRResponse&#46;status == 200 ? "Updated" : "Some error while updating", "text&#47;plain");
} else {
response&#46;success("User was not found", "text&#47;plain");
}/pre


Forgot Password Service

Posted: Thu Dec 05, 2013 7:12 pm
by fender

XHR! Yes, yes, yes. I see it now. Thanks


Forgot Password Service

Posted: Fri Dec 06, 2013 1:37 am
by fender

This may not be secure? And it's built from the contributions to this thread - Thanks people. It uses the username from the user database as input and resets the password to a random string, gets the email address from the user database, and sends an email to the user with the new password.

var username = request.get("username");
var responseBody = {};
var dbId = 'get this from the database';
var masterKey = "get this from the database";
var tempPassword = Math.random().toString(36).slice(-12);
//find user with username from request parameter
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/users/", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": encodeURIComponent('{"username": "' + username + '"}')
}
});
if (XHRResponse.body.length) { //if user exists
var userId = XHRResponse.body[0]["_id"];
var email = XHRResponse.body[0]["email"];
//update user info
var XHRResponse = XHR.send("PUT", "https://api.appery.io/rest/1/db/users/" + userId, {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey,
"Content-Type": "application/json"
},
"body": {
"password": tempPassword
}
});
if (XHRResponse.status == 200) {
var XHRResponse = XHR.send("POST", "https://api.sendgrid.com/api/mail.sen...", {
"parameters": {
"api_user": "your sendgrid user name",
"api_key": "your sendgrid password",
"to": email,
"toname": "",
"subject": "subject text",
"text": "Your password reset request has been completed. Your new password is: " + tempPassword,
"from": "a href="mailto:noreply@domain.com" rel="nofollow"noreply@domain.com/a"
}
});

Code: Select all

 responseBody.message = "Your password has been reset. An email with the new password has been sent to you."; 

} else {
responseBody.message = "Database Error - unkown";
}
} else {
responseBody.message = "User ID not found";
}
response.success(responseBody, "application/json");


Forgot Password Service

Posted: Fri Dec 06, 2013 11:01 am
by Maryna Brodina

Hello! Does it work for you or you have some problem?


Forgot Password Service

Posted: Fri Dec 06, 2013 11:13 am
by Doug Black

Yeah, does this work? This looks much better than my code!