summaryrefslogtreecommitdiff
path: root/www/app/models
diff options
context:
space:
mode:
authorKali Kaneko (leap communications) <kali@leap.se>2016-09-01 01:44:34 -0400
committerKali Kaneko (leap communications) <kali@leap.se>2016-09-01 01:44:34 -0400
commit4728855b40d2d37da8e035c5081fab5819b07fd0 (patch)
treec8a7c727521fe06a2c4fe9221cb77d30e8f0c3b9 /www/app/models
parent4613e74ce4e2c8b125a6a61585a4aec5f5151969 (diff)
[refactor] move js to top-level folder
Diffstat (limited to 'www/app/models')
-rw-r--r--www/app/models/account.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/www/app/models/account.js b/www/app/models/account.js
new file mode 100644
index 0000000..367961b
--- /dev/null
+++ b/www/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 == '<none>') {
+ return null
+ } else {
+ return new Account(response.user, {authenticated: true})
+ }
+ }
+ )
+ }
+
+} \ No newline at end of file