From 89e9d840565c18463b2643920032b21ba232796b Mon Sep 17 00:00:00 2001 From: elijah Date: Fri, 26 Aug 2016 21:09:53 -0700 Subject: [feat] added initial bitmask_js (WIP) --- src/leap/bitmask_js/app/models/account.js | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/leap/bitmask_js/app/models/account.js (limited to 'src/leap/bitmask_js/app/models/account.js') diff --git a/src/leap/bitmask_js/app/models/account.js b/src/leap/bitmask_js/app/models/account.js new file mode 100644 index 00000000..367961bf --- /dev/null +++ b/src/leap/bitmask_js/app/models/account.js @@ -0,0 +1,88 @@ +// +// An account is an abstraction of a user and a provider. +// The user part is optional, so an Account might just represent a provider. +// + +import bitmask from 'lib/bitmask' + +export default class Account { + + constructor(address, props={}) { + if (!address.match('@')) { + this._address = '@' + address + } else { + this._address = address + } + this._authenticated = props.authenticated + } + + // + // currently, bitmask.js uses address for id, so we return address here too. + // also, we don't know uuid until after authentication. + // + // TODO: change to uuid when possible. + // + get id() { + return this._address + } + + get domain() { + return this._address.split('@')[1] + } + + get address() { + return this._address + } + + get userpart() { + return this._address.split('@')[0] + } + + get authenticated() { + return this._authenticated + } + + // + // returns a promise, fulfill is passed account object + // + login(password) { + return bitmask.user.auth(this.address, password).then( + response => { + if (response.uuid) { + this._uuid = response.uuid + this._authenticated = true + } + return this + } + ) + } + + // + // returns a promise, fulfill is passed account object + // + logout() { + return bitmask.user.logout(this.id).then( + response => { + this._authenticated = false + this._address = '@' + this.domain + return this + } + ) + } + + // + // returns a promise, fullfill is passed account object + // + static active() { + return bitmask.user.active().then( + response => { + if (response.user == '') { + return null + } else { + return new Account(response.user, {authenticated: true}) + } + } + ) + } + +} \ No newline at end of file -- cgit v1.2.3