|
// -------------------------------------------------------------------------------------------------------------------- |
|
|
|
// npm |
|
var prompt = require('prompt') |
|
var P = require('p-promise') |
|
|
|
// local |
|
var FxAClient = require('./') |
|
|
|
// -------------------------------------------------------------------------------------------------------------------- |
|
|
|
var email = ('' + Math.random()).substr(2) + '@restmail.net' |
|
var password = 'my password' |
|
|
|
// to be filled in as we go along |
|
var uid |
|
var sessionToken |
|
var code |
|
var passwordForgotToken |
|
var passwordForgotCode |
|
|
|
// the FxA Client |
|
var client = new FxAClient('http://localhost:9000') |
|
|
|
// -------------------------------------------------------------------------------------------------------------------- |
|
|
|
client.signUp(email, password) |
|
.then(function(res) { |
|
title('signUp') |
|
dump(res) |
|
uid = res.uid |
|
sessionToken = res.sessionToken |
|
|
|
var d = P.defer() |
|
|
|
// get the code from the prompt |
|
prompt.start() |
|
prompt.get('code', function(err, res) { |
|
if (err) return d.reject(err) |
|
code = res.code |
|
|
|
// now, verify this code |
|
var p = client.verifyCode(uid, code) |
|
return d.resolve(p) |
|
}) |
|
|
|
return d.promise |
|
}) |
|
.then(function(res) { |
|
title('verifyCode') |
|
dump(res) |
|
|
|
// start a password reset flow |
|
return client.passwordForgotSendCode(email) |
|
}) |
|
.then(function(res) { |
|
title('passwordForgotSendCode') |
|
dump(res) |
|
|
|
passwordForgotToken = res.passwordForgotToken |
|
console.log('passwordForgotToken:', passwordForgotToken) |
|
|
|
// prompt for this code |
|
var d = P.defer() |
|
|
|
prompt.start() |
|
prompt.get('code', function(err, res) { |
|
if (err) return d.reject(err) |
|
passwordForgotCode = res.code |
|
|
|
console.log('passwordForgotCode:', passwordForgotCode) |
|
|
|
// verify the password code |
|
var p = client.passwordForgotVerifyCode(passwordForgotCode, passwordForgotToken) |
|
return d.resolve(p) |
|
}) |
|
|
|
return d.promise |
|
}) |
|
.then(function(res) { |
|
title('passwordForgotVerifyCode') |
|
dump(res) |
|
|
|
// do something ... once we get the above working ... |
|
return true |
|
}) |
|
.done(function(result) { |
|
title('Finished') |
|
dump(result) |
|
title('---') |
|
}, function(err) { |
|
title('Error') |
|
dump(err) |
|
}) |
|
|
|
// -------------------------------------------------------------------------------------------------------------------- |
|
// utility functions |
|
|
|
function title(str) { |
|
console.log('--- %s ---', str) |
|
} |
|
|
|
function dump(data) { |
|
console.log(JSON.stringify(data, null, ' ')) |
|
} |
|
|
|
// -------------------------------------------------------------------------------------------------------------------- |