[494] | 1 | define([ |
---|
| 2 | "dojo/Deferred", |
---|
| 3 | "dojo/Evented", |
---|
| 4 | "dojo/_base/declare", |
---|
| 5 | "dojo/_base/json", |
---|
| 6 | "dojo/_base/lang", |
---|
| 7 | "dojo/request" |
---|
| 8 | ], function(Deferred, Evented, declare, json, lang, request) { |
---|
| 9 | var Session = declare([Evented], { |
---|
| 10 | info: null, |
---|
| 11 | get: function() { |
---|
| 12 | return this.info; |
---|
| 13 | }, |
---|
| 14 | restore: function() { |
---|
| 15 | return request('/api/login', { |
---|
| 16 | method: "GET", |
---|
| 17 | handleAs: "json" |
---|
| 18 | }).then(lang.hitch(this,function(res) { |
---|
| 19 | return this._set(res); |
---|
| 20 | }), lang.hitch(this,function() { |
---|
| 21 | return new Deferred().reject(this._set(null)); |
---|
| 22 | })); |
---|
| 23 | }, |
---|
| 24 | login: function(username, password) { |
---|
| 25 | return request('/api/login', { |
---|
| 26 | method: "POST", |
---|
| 27 | handleAs: "json", |
---|
| 28 | data: { |
---|
| 29 | username: username, |
---|
| 30 | password: password |
---|
| 31 | } |
---|
| 32 | }).then(lang.hitch(this,function(res) { |
---|
| 33 | return this._set(res); |
---|
| 34 | }), lang.hitch(this,function() { |
---|
| 35 | return new Deferred().reject(this._set(null)); |
---|
| 36 | })); |
---|
| 37 | }, |
---|
| 38 | logout: function() { |
---|
| 39 | return request('/api/logout', { |
---|
| 40 | method: "POST", |
---|
| 41 | handleAs: "json" |
---|
| 42 | }).then(lang.hitch(this,function(res) { |
---|
| 43 | return this._set(null); |
---|
| 44 | }), lang.hitch(this,function() { |
---|
| 45 | return this._set(null); |
---|
| 46 | })); |
---|
| 47 | }, |
---|
| 48 | _set: function(newInfo) { |
---|
| 49 | if (newInfo !== this.info) { |
---|
| 50 | this.info = newInfo; |
---|
| 51 | this.emit('change', this.info); |
---|
| 52 | } |
---|
| 53 | return this.info; |
---|
| 54 | } |
---|
| 55 | }); |
---|
| 56 | |
---|
| 57 | return new Session(); |
---|
| 58 | }); |
---|