This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
69 lines (58 loc) · 2.08 KB
/
client.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
var request = require('request')
var url = 'http://127.0.0.1:4343/'
var creds = {
email: 'hi@example.com',
password: 'badpassword'
}
// user registers
register(creds, function (err, account) {
if (err) return console.log(err)
// they forget their password and initiate the reset flow
reset({ accountKey: account.key }, function (err, info) {
if (err) return console.log(err)
// pretend like i've been redirected from the email to a form
// and this is what's being sent from the form
var options = {
accountKey: account.key,
password: 'badpassword',
newPassword: 'betterpasswordkinda',
resetToken: info.resetToken
}
// user confirms reset with new password
confirm(options, function (err) {
if (err) return console.log(err)
// user logs in with new password
login({ email: 'hi@example.com', password: 'betterpasswordkinda' }, function (err) {
if (err) return console.log(err)
})
})
})
})
function register (creds, callback) {
request({ url: url + 'account', json: true, body: creds, method: 'POST' }, function (err, res, body) {
if (err) return callback(err)
if (res.statusCode >= 400) return callback(body)
return callback(null, body)
})
}
function reset (options, callback) {
request({ url: url + 'reset/' + options.accountKey, json: true, method: 'POST' }, function (err, res, body) {
if (err) return callback(err)
if (res.statusCode >= 400) return callback(body)
return callback(null, body)
})
}
function confirm (options, callback) {
request({ url: url + 'reset/' + options.accountKey + '/' + options.resetToken, json: true, body: options, method: 'POST' }, function (err, res, body) {
if (err) return callback(err)
if (res.statusCode >= 400) return callback(body)
return callback(null, body)
})
}
function login (creds, callback) {
request({ url: url + 'login', json: true, body: creds, method: 'POST' }, function (err, res, body) {
if (err) return callback(err)
if (res.statusCode >= 400) return callback(body)
return callback(null, body)
})
}