removed callbacks from client lib
This commit is contained in:
Родитель
8dde73becb
Коммит
9d5547dba5
214
client/index.js
214
client/index.js
|
@ -87,7 +87,7 @@ Client.prototype.setupCredentials = function (email, password, customSalt, custo
|
|||
)
|
||||
}
|
||||
|
||||
Client.create = function (origin, email, password, options, callback) {
|
||||
Client.create = function (origin, email, password, options) {
|
||||
var c = new Client(origin)
|
||||
options = options || {}
|
||||
if (typeof(options) === 'function') {
|
||||
|
@ -99,57 +99,38 @@ Client.create = function (origin, email, password, options, callback) {
|
|||
c.lang = options.lang
|
||||
}
|
||||
|
||||
var p = c.setupCredentials(email, password)
|
||||
return c.setupCredentials(email, password)
|
||||
.then(
|
||||
function() {
|
||||
return c.create()
|
||||
}
|
||||
)
|
||||
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.login = function (origin, email, password, callback) {
|
||||
Client.login = function (origin, email, password) {
|
||||
var c = new Client(origin)
|
||||
c.email = Buffer(email).toString('hex')
|
||||
c.password = password
|
||||
|
||||
var p = c.login()
|
||||
return c.login()
|
||||
.then(
|
||||
function () {
|
||||
return c
|
||||
}
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.changePassword = function (origin, email, oldPassword, newPassword, callback) {
|
||||
Client.changePassword = function (origin, email, oldPassword, newPassword) {
|
||||
var c = new Client(origin)
|
||||
c.email = Buffer(email).toString('hex')
|
||||
c.password = oldPassword
|
||||
|
||||
var p = c.changePassword(newPassword)
|
||||
return c.changePassword(newPassword)
|
||||
.then(
|
||||
function () {
|
||||
return c
|
||||
}
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.parse = function (string) {
|
||||
|
@ -172,8 +153,8 @@ Client.parse = function (string) {
|
|||
return client
|
||||
}
|
||||
|
||||
Client.prototype.create = function (callback) {
|
||||
var p = this.api.accountCreate(
|
||||
Client.prototype.create = function () {
|
||||
return this.api.accountCreate(
|
||||
this.email,
|
||||
this.srp.verifier,
|
||||
this.srp.salt,
|
||||
|
@ -191,18 +172,12 @@ Client.prototype.create = function (callback) {
|
|||
lang: this.lang
|
||||
}
|
||||
)
|
||||
.then(
|
||||
function (a) {
|
||||
this.uid = a.uid
|
||||
return this
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
.then(
|
||||
function (a) {
|
||||
this.uid = a.uid
|
||||
return this
|
||||
}.bind(this)
|
||||
)
|
||||
}
|
||||
|
||||
Client.prototype._clear = function () {
|
||||
|
@ -221,11 +196,11 @@ Client.prototype.stringify = function () {
|
|||
return JSON.stringify(this)
|
||||
}
|
||||
|
||||
Client.prototype.accountExists = function (email, callback) {
|
||||
Client.prototype.accountExists = function (email) {
|
||||
if (email) {
|
||||
this.email = Buffer(email).toString('hex')
|
||||
}
|
||||
var p = this.api.authStart(this.email)
|
||||
return this.api.authStart(this.email)
|
||||
.then(
|
||||
function (srpSession) {
|
||||
this.srpSession = srpSession
|
||||
|
@ -239,19 +214,13 @@ Client.prototype.accountExists = function (email, callback) {
|
|||
}
|
||||
}
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
};
|
||||
|
||||
Client.prototype.auth = function (callback) {
|
||||
Client.prototype.auth = function () {
|
||||
var K = null
|
||||
var session
|
||||
var sessionPromise = this.srpSession ? P(this.srpSession) : this.api.authStart(this.email)
|
||||
var p = sessionPromise
|
||||
return sessionPromise
|
||||
.then(
|
||||
function (srpSession) {
|
||||
var k = P.defer()
|
||||
|
@ -299,16 +268,10 @@ Client.prototype.auth = function (callback) {
|
|||
return authToken
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.login = function (callback) {
|
||||
var p = this.auth()
|
||||
Client.prototype.login = function () {
|
||||
return this.auth()
|
||||
.then(
|
||||
function () {
|
||||
return this.api.sessionCreate(this.authToken)
|
||||
|
@ -333,16 +296,9 @@ Client.prototype.login = function (callback) {
|
|||
return tokens
|
||||
}.bind(this)
|
||||
)
|
||||
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.destroySession = function (callback) {
|
||||
Client.prototype.destroySession = function () {
|
||||
var p = P(null)
|
||||
if (this.sessionToken) {
|
||||
p = this.api.sessionDestroy(this.sessionToken)
|
||||
|
@ -353,27 +309,16 @@ Client.prototype.destroySession = function (callback) {
|
|||
}.bind(this)
|
||||
)
|
||||
}
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
Client.prototype.verifyEmail = function (code, callback) {
|
||||
var p = this.api.recoveryEmailVerifyCode(this.uid, code)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
Client.prototype.verifyEmail = function (code) {
|
||||
return this.api.recoveryEmailVerifyCode(this.uid, code)
|
||||
}
|
||||
|
||||
Client.prototype.emailStatus = function (callback) {
|
||||
Client.prototype.emailStatus = function () {
|
||||
var o = this.sessionToken ? P(null) : this.login()
|
||||
var p = o.then(
|
||||
return o.then(
|
||||
function () {
|
||||
return this.api.recoveryEmailStatus(this.sessionToken)
|
||||
}.bind(this)
|
||||
|
@ -385,32 +330,20 @@ Client.prototype.emailStatus = function (callback) {
|
|||
return status
|
||||
}
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.requestVerifyEmail = function (callback) {
|
||||
Client.prototype.requestVerifyEmail = function () {
|
||||
var o = this.sessionToken ? P(null) : this.login()
|
||||
var p = o.then(
|
||||
return o.then(
|
||||
function () {
|
||||
return this.api.recoveryEmailResendCode(this.sessionToken)
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.sign = function (publicKey, duration, callback) {
|
||||
Client.prototype.sign = function (publicKey, duration) {
|
||||
var o = this.sessionToken ? P(null) : this.login()
|
||||
var p = o.then(
|
||||
return o.then(
|
||||
function () {
|
||||
return this.api.certificateSign(this.sessionToken, publicKey, duration)
|
||||
}.bind(this)
|
||||
|
@ -420,16 +353,10 @@ Client.prototype.sign = function (publicKey, duration, callback) {
|
|||
return x.cert
|
||||
}
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.changePassword = function (newPassword, callback) {
|
||||
var p = this.auth()
|
||||
Client.prototype.changePassword = function (newPassword) {
|
||||
return this.auth()
|
||||
.then(
|
||||
function () {
|
||||
return this.api.passwordChangeStart(this.authToken)
|
||||
|
@ -502,17 +429,11 @@ Client.prototype.changePassword = function (newPassword, callback) {
|
|||
}.bind(this)
|
||||
)
|
||||
.then(this._clear.bind(this))
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.keys = function (callback) {
|
||||
Client.prototype.keys = function () {
|
||||
var o = this.keyFetchToken ? P(null) : this.login()
|
||||
var p = o.then(
|
||||
return o.then(
|
||||
function () {
|
||||
return this.api.accountKeys(this.keyFetchToken)
|
||||
}.bind(this)
|
||||
|
@ -541,18 +462,11 @@ Client.prototype.keys = function (callback) {
|
|||
throw err
|
||||
}.bind(this)
|
||||
)
|
||||
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.devices = function (callback) {
|
||||
Client.prototype.devices = function () {
|
||||
var o = this.sessionToken ? P(null) : this.login()
|
||||
var p = o.then(
|
||||
return o.then(
|
||||
function () {
|
||||
return this.api.accountDevices(this.sessionToken)
|
||||
}.bind(this)
|
||||
|
@ -563,78 +477,48 @@ Client.prototype.devices = function (callback) {
|
|||
return this._devices
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.destroyAccount = function (callback) {
|
||||
var p = this.auth()
|
||||
Client.prototype.destroyAccount = function () {
|
||||
return this.auth()
|
||||
.then(
|
||||
function () {
|
||||
return this.api.accountDestroy(this.authToken)
|
||||
}.bind(this)
|
||||
)
|
||||
.then(this._clear.bind(this))
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.forgotPassword = function (callback) {
|
||||
Client.prototype.forgotPassword = function () {
|
||||
this._clear()
|
||||
var p = this.api.passwordForgotSendCode(this.email)
|
||||
return this.api.passwordForgotSendCode(this.email)
|
||||
.then(
|
||||
function (x) {
|
||||
this.forgotPasswordToken = x.forgotPasswordToken
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.reforgotPassword = function (callback) {
|
||||
var p = this.api.passwordForgotResendCode(this.forgotPasswordToken, this.email)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
Client.prototype.reforgotPassword = function () {
|
||||
return this.api.passwordForgotResendCode(this.forgotPasswordToken, this.email)
|
||||
}
|
||||
|
||||
Client.prototype.verifyPasswordResetCode = function (code, callback) {
|
||||
var p = this.api.passwordForgotVerifyCode(this.forgotPasswordToken, code)
|
||||
Client.prototype.verifyPasswordResetCode = function (code) {
|
||||
return this.api.passwordForgotVerifyCode(this.forgotPasswordToken, code)
|
||||
.then(
|
||||
function (result) {
|
||||
this.accountResetToken = result.accountResetToken
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
Client.prototype.resetPassword = function (newPassword, callback) {
|
||||
Client.prototype.resetPassword = function (newPassword) {
|
||||
if (!this.accountResetToken) {
|
||||
throw new Error("call verifyPasswordResetCode before calling resetPassword");
|
||||
}
|
||||
// this will generate a new wrapKb on the server
|
||||
var wrapKb = NULL
|
||||
var p = this.setupCredentials(this.email, newPassword)
|
||||
return this.setupCredentials(this.email, newPassword)
|
||||
.then(
|
||||
tokens.AccountResetToken.fromHex.bind(null, this.accountResetToken)
|
||||
)
|
||||
|
@ -664,12 +548,6 @@ Client.prototype.resetPassword = function (newPassword, callback) {
|
|||
)
|
||||
}.bind(this)
|
||||
)
|
||||
if (callback) {
|
||||
p.done(callback.bind(null, null), callback)
|
||||
}
|
||||
else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
//TODO recovery methods, session status/destroy
|
||||
|
|
Загрузка…
Ссылка в новой задаче