Last active
August 29, 2015 14:01
-
-
Save rdegges/13ceb8ef0abb7bd7ae60 to your computer and use it in GitHub Desktop.
Stormpath Node.js Quickstart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* quickstart.js | |
* ~~~~~~~~~~~~~ | |
* | |
* Code from the Stormpath Node.js Quickstart: | |
* http://docs.stormpath.com/nodejs/quickstart/ | |
* | |
* You can run this code by typing: | |
* | |
* $ node quickstart.js | |
* | |
* In your terminal. | |
* | |
* Questions? Email us! [email protected] | |
*/ | |
var stormpath = require('stormpath'); | |
var client = null; | |
var homedir = (process.platform === 'win32') ? process.env.HOMEPATH : process.env.HOME; | |
var keyfile = homedir + '/.stormpath/apiKey.properties'; | |
// Create a Stormpath Client. | |
stormpath.loadApiKey(keyfile, function apiKeyFileLoaded(err, apiKey) { | |
if (err) throw err; | |
client = new stormpath.Client({apiKey: apiKey}); | |
console.log('Created client!'); | |
// Get the Stormpath Application. | |
client.getApplications(function(err, apps) { | |
if (err) throw err; | |
var app = apps.items[0]; | |
console.log('Application retrieved!'); | |
var account = { | |
givenName: 'Joe', | |
surname: 'Stormtrooper', | |
username: 'tk455', | |
email: '[email protected]', | |
password: 'Changeme1', | |
customData: { | |
favoriteColor: 'white', | |
}, | |
}; | |
// Create a Stormpath Account. | |
app.createAccount(account, function(err, account) { | |
if (err) throw err; | |
console.log('Account created!'); | |
console.log('Account givenName: ' + account.givenName); | |
console.log('Account surname: ' + account.surname); | |
// Search for Account by email. | |
app.getAccounts({email: '[email protected]'}, function(err, accounts) { | |
if (err) throw err; | |
accounts.each(function (err, account, index) { | |
console.log('Found account using search by email!'); | |
}); | |
}); | |
// Authenticate Account by username / password. | |
app.authenticateAccount({ | |
username: 'tk455', | |
password: 'Changeme1', | |
}, function (err, result) { | |
if (err) throw err; | |
console.log('Successfully authenticated account using username!'); | |
}); | |
// Authenticate Account by email / password. | |
app.authenticateAccount({ | |
username: '[email protected]', | |
password: 'Changeme1', | |
}, function (err, result) { | |
if (err) throw err; | |
console.log('Successfully authenticated account using email!'); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment