function login(username, password, callback) {
// Replace the {yourStormpathClientId} attribute with your Stormpath ID
var url = 'https://api.stormpath.com/v1/applications/{yourStormpathClientId}/loginAttempts';
// Stormpath requires the user credentials be passed in as a base64 encoded message
var message = username + ':' + password;
var pass = new Buffer(message).toString('base64');
// Here we are making the POST request to authenticate a user
request({
url: url,
method: 'POST',
auth: {
// Your API Client ID
user: '{yourStormpathClientId}',
// Your API Client Secret
password: '{yourStormpathClientSecret}'
},
headers: {
'Content-Type': 'application/json'
},
json: {
type: 'basic',
// Passing in the base64 encoded credentials
value: pass
}
}, function (error, response, body) {
// If response is successful we'll continue
if (response.statusCode !== 200) return callback();
// A successful response will return a URL to get the user information
var accountUrl = body.account.href;
// We'll make a second request to get the user info. This time it will be a GET request
request({
url: accountUrl,
method: 'GET',
auth: {
// Your API Client ID
user: '{yourStormpathClientId}',
// YOUR API Client Secret
password: '{yourStormpathClientSecret}'
}
}, function (errorUserInfo, responseUserInfo, bodyUserInfo) {
// If we get a successful response, we'll process it
if (responseUserInfo.statusCode !== 200) return callback();
var parsedBody = JSON.parse(bodyUserInfo);
// To get the user identifier, we'll strip out the Stormpath API
var id = parsedBody.href.replace('https://api.stormpath.com/v1/accounts/', '');
// Finally, we'll set the data we want to store in Auth0 and migrate the user
return callback(null, {
user_id : id,
username: parsedBody.username,
email: parsedBody.email,
// We set the users email_verified to true as we assume if they were a valid
// user in Stormpath, they have already verified their email
// If this field is not set, the user will get an email asking them to verify
// their account
email_verified: true,
// Add any additional fields you would like to carry over from Stormpath
});
});
});
}