This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathquickstart.js
205 lines (160 loc) · 5.92 KB
/
quickstart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict';
// ==================================================
// This file is intended to reflect the Stormpath
// Node.js Quickstart documented here:
//
// http://docs.stormpath.com/nodejs/api/
//
// Enjoy!
// ==================================================
var stormpath = require('stormpath');
//helper function to prevent any data collisions in the tenant while running the quickstart:
function unique(aString) {
return aString + '-' + require('uuid').v4().toString();
}
//populated during the quickstart steps
var client, application, account, group = null;
var accountEmail = unique('jlpicard') + '@mailinator.com';
// ==================================================
// Step 1 - Create a client and wait for it to ready
// ==================================================
var client = new stormpath.Client();
client.on('ready', createApplication);
// ==================================================
// Step 2 - Register an application with Stormpath
// ==================================================
function createApplication() {
var app = {
name: unique('My Awesome Application'),
description: ('No, Srsly. It\'s Awesome')
};
return client.createApplication(app, {createDirectory: true}, function(err, app) {
if (err) throw err;
application = app;
console.log('Created application:');
console.log(application);
return createAccount(); //next quickstart step
});
}
// ==================================================
// Step 3 - Create a new application account
// ==================================================
function createAccount() {
var acct = {
givenName: 'Jean-Luc',
surname: 'Picard',
username: unique('jlpicard'),
email: accountEmail,
password: 'Changeme1!'
};
return application.createAccount(acct, function(err, createdAccount) {
if (err) throw err;
account = createdAccount;
console.log('Created account:');
console.log(account);
return updateAccount(); //next quickstart step
});
}
// ==================================================
// Step 3 - Update an account
// ==================================================
function updateAccount() {
account.middleName = 'Make it so.';
return account.save(function(err, savedAccount) {
if (err) throw err;
account = savedAccount;
console.log('Updated account:');
console.log(account);
return authenticateAccount(); //next quickstart step
});
}
// ==================================================
// Step 4 - Authenticate an account
// ==================================================
function authenticateAccount() {
var authcRequest = {
username: accountEmail, //we could've entered the username instead
password: 'Changeme1!'
};
return application.authenticateAccount(authcRequest, function(err, result) {
if (err) throw err;
return result.getAccount(function(err2, theAccount) { //this is cached and will execute immediately (no server request):
if(err2) throw err2;
account = theAccount;
console.log('Authenticated account:');
console.log(account);
//authentication was successful, so we ordinarily have no need to send a
//password reset email right now, but hey - this is a quickstart and we're
//trying to show stuff! So kick off the password reset workflow just for fun:
return sendPasswordResetEmail(); //next quickstart step
});
});
}
// ==================================================
// Step 5 - Send the account a password reset email
// ==================================================
function sendPasswordResetEmail() {
return application.sendPasswordResetEmail(accountEmail, function(err, token) {
if (err) throw err;
console.log("Sent email that included a link with password reset token: " + token.toString());
console.log("If you like, go to mailinator.com and use this email address to view the email: " + accountEmail);
//ok, moving on with the quickstart:
return createGroup();
});
}
// ==================================================
// Step 6 - Create a group
// ==================================================
function createGroup() {
var aGroup = {
name: unique('Administrators')
};
return application.createGroup(aGroup, function(err, createdGroup) {
if (err) throw err;
group = createdGroup;
return addAccountToGroup(); //next quickstart step
});
}
// ==================================================
// Step 7 - Add an Account to a Group
// ==================================================
function addAccountToGroup() {
return account.addToGroup(group, function(err, membership) {
if (err) throw err;
console.log(membership);
return retrieveAccountGroups(); //next quickstart step
});
}
// ==================================================
// Step 8 - Retrieve an Account's Groups
// ==================================================
function retrieveAccountGroups() {
account.getGroups(function(err, groups) {
groups.each(function(err, group) {
console.log(group);
})
});
//finish quickstart:
cleanup();
}
// ==================================================
// End of quickstart - cleanup quickstart data
// ==================================================
//deletes the objects we created during the quickstart so we don't pollute the tenant
function cleanup() {
var ds = application.dataStore;
//delete the application's directory. This will auto-delete any contained accounts and groups:
ds.getResource(application.defaultAccountStoreMapping.href, function(err, mapping) {
if (err) throw err;
console.log('Retrieved application accountStoreMapping for deletion:');
console.log(mapping);
ds.deleteResource(mapping.accountStore, function(err) {
if(err) throw err;
console.log('Deleted application directory.');
application.delete(function(err) {
if (err) throw err;
console.log('Deleted application.');
});
});
});
}