Skip to content

Instantly share code, notes, and snippets.

@chilts
Created March 26, 2015 02:37
Show Gist options
  • Save chilts/dfd5134b403e3afa5750 to your computer and use it in GitHub Desktop.
Save chilts/dfd5134b403e3afa5750 to your computer and use it in GitHub Desktop.
$ node client.js
--- signUp ---
{
"uid": "8bc2add5218d4f10971a139a54ad6a4f",
"sessionToken": "00b4e46c5e94fdec7726ea1bc145363ba8174c8794d470134b6bf1f1364ce5d6",
"authAt": 1427337145
}
prompt: code: fdfe175d9f20fa0d3e1d8e7a61dfa481
--- verifyCode ---
{}
--- passwordForgotSendCode ---
{
"passwordForgotToken": "1bb42e9a97904ebd4bd70c9c9ffc7ab3f042d699b47e288950c127e76ebba5a7",
"ttl": 3600,
"codeLength": 16,
"tries": 3
}
passwordForgotToken: 1bb42e9a97904ebd4bd70c9c9ffc7ab3f042d699b47e288950c127e76ebba5a7
prompt: code: 3ab8c66bfebf80eaa1dfcdfccf60591c
passwordForgotCode: 3ab8c66bfebf80eaa1dfcdfccf60591c
--- Error ---
{
"code": 401,
"errno": 109,
"error": "Unauthorized",
"message": "Bad mac",
"info": "https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#response-format",
"log": []
}
// --------------------------------------------------------------------------------------------------------------------
// 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, ' '))
}
// --------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment