Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
implement more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert committed Jul 3, 2014
1 parent e1f4c8e commit 687055a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 24 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,36 +203,38 @@ client.sendPasswordResetEmail(email,function(err,result){

If the user has clicked on the password reset link that we sent them,
the `client` will automatically fetch the password reset token from the URL.
You should then call `verifyPasswordResetToken` to initiate the call to
Stormpath which will verify the integrity of the token:
You should then call `verifyPasswordResetToken` to verify the token with Stormpath's API:

````javascript
client.verifyPasswordToken(function(err,account){
client.verifyPasswordToken(function(err,pwTokenVerification){
if(err){
// token is invalid, expired, or already used.
// show err.userMessage to user
}else{
// prompt the user for a new password, then
// save it to the account
// call setNewPassword
}
})
````

### Set a new password

After verifying the password reset token, collect a new password
from the user, set it on the `account` that was returned during verification,
then call `save()` on the account:
After verifying the password reset token and receiving a `pwTokenVerification`,
collect a new password and pass it with the verification to `setNewPassword`.

**NOTE**: You may only make one setPassword request per session. You must
use client-side validation to parse the `passwordPolicy` and proactively
warn the user that their password is not correct, before you invoke this
method


````javascript

account.password = "hackerZtheplanet!!"
account.save(function(err){
client.setNewPassword(pwTokenVerification,newPassword,function(err,result){
if(err){
// password strength rules were not met,
// show err.userMessage to the user
// password strength rules were not met
}else{
// success, now prompt the user to login
}
})
});
````
82 changes: 70 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Client(options,readyCallback){
var opts = typeof options === 'object' ? options : {};
var cb = typeof options === 'function' ? options : ( readyCallback || utils.noop);
var self = this;

self.baseurl = "https://api.stormpath.com";
self.jwt = opts.jwt || (window.location.href.match(/jwt=(.+)/) || [])[1];
if(!self.jwt){
return cb(new Error('jwt not found as url query parameter'));
Expand All @@ -29,26 +29,84 @@ function Client(options,readyCallback){

Client.prototype.login = function login(credentials,callback) {
var self = this;
var data;
if(!credentials){
callback(new Error('must provide an object'));
return callback(new Error('must provide an object'));
}else if(credentials.providerData){
// todo
data = credentials;
}else if(credentials.login){
var data = {
data = {
type: 'basic',
value: utils.base64.btoa(credentials.login + ':' + credentials.password)
};
self.requestExecutor.execute(
'POST',self.appHref+'/loginAttempts',
{
body: data
},
callback
);
}else{
callback(new Error('unsupported credentials object'));
return callback(new Error('unsupported credentials object'));
}

self.requestExecutor.execute(
'POST',self.appHref+'/loginAttempts',
{
body: data
},
callback
);

};

Client.prototype.register = function register(data,callback) {
var self = this;
self.requestExecutor.execute(
'POST',self.appHref+'/accounts',
{
body: data
},
callback
);
};

Client.prototype.verifyEmailToken = function verifyEmailToken(callback) {
var self = this;
self.requestExecutor.execute(
'POST',
self.baseurl + '/v1/accounts/emailVerificationTokens/' + self.sptoken,
callback
);
};

Client.prototype.verifyPasswordResetToken = function verifyEmailToken(callback) {
var self = this;
self.requestExecutor.execute(
'GET',
self.appHref + '/passwordResetTokens/' + self.sptoken,
callback
);
};

Client.prototype.setNewPassword = function setNewPassword(pwTokenVerification,password,callback) {
if(!pwTokenVerification || !pwTokenVerification.href){
return callback(new Error('invalid pwTokenVerification'));
}
var self = this;
self.requestExecutor.execute('POST',pwTokenVerification.href,
{
body: {
password: password
}
},
callback
);
};

Client.prototype.sendPasswordResetEmail = function sendPasswordResetEmail(emailOrUsername,callback) {
var self = this;
self.requestExecutor.execute(
'POST',
self.appHref + '/passwordResetTokens',
{
body: { email: emailOrUsername }
},
callback
);
};

module.exports = Client;

0 comments on commit 687055a

Please sign in to comment.