UNPKG

3.5 kBJavaScriptView Raw
1import RESTAdapter from '../-lib/user/adapters/rest';
2
3import { isPresent, isEmpty } from '@ember/utils';
4import { getWithDefault } from '@ember/object';
5import { readOnly } from '@ember/object/computed';
6
7/**
8 * @class AccountRESTAdapter
9 *
10 * The RESTAdapter for the account model.
11 */
12export default RESTAdapter.extend({
13 /// Get the host from the base url for the client.
14 host: readOnly ('session.gatekeeper.baseUrl'),
15
16 createRecord (store, type, snapshot) {
17 let _super = this._super;
18 let adapter = this;
19 let { adapterOptions } = snapshot;
20 let opts = {};
21
22 if (isPresent (Object.keys (adapterOptions))) {
23 Object.assign (opts, adapterOptions);
24
25 if (opts.signIn) {
26 delete opts.signIn;
27 }
28 }
29
30 // There is a chance that we are creating an account for one that already exists. We need
31 // to remove the account that matches this email address from our cache. Otherwise, if we
32 // do create an account from an existing email address that we have cached on the device,
33 // this request will fail.
34
35 const email = snapshot.attr ('email');
36 const account = store.peekAll ('account').find (account => account.email === email);
37
38 if (isPresent (account)) {
39 if (!account.isSaving) {
40 account.unloadRecord ();
41 }
42 }
43
44 return this.get ('session.gatekeeper').authenticate (opts).then (() => {
45 return _super.call (adapter, store, type, snapshot);
46 });
47 },
48
49 urlForCreateRecord (modelName, snapshot) {
50 let url = this._super (...arguments);
51
52 let signIn = getWithDefault (snapshot, 'adapterOptions.signIn', false);
53
54 if (signIn) {
55 url += '?login=true';
56 }
57
58 return url;
59 },
60
61 urlForQueryRecord (query, modelName) {
62 return isEmpty (Object.keys (query)) ? this.buildURL (modelName, 'me', null, 'findRecord', null) : this._super (...arguments);
63 },
64
65 ajaxOptions (url /*, type, options*/) {
66 let hash = this._super (...arguments);
67 let beforeSend = hash.beforeSend || function (/*xhr*/) { };
68
69 hash.beforeSend = function (xhr) {
70 // Call the original beforeSend() function.
71 beforeSend (xhr);
72
73 if (endsWith (url, '/me')) {
74 // For request about the current account, we do not want to cache the response.
75 // Otherwise, we run the risk of caching data related to a different user.
76 xhr.setRequestHeader ('Cache-Control', 'no-cache, no-store, must-revalidate');
77 }
78 };
79
80 return hash;
81 },
82
83 handleResponse (status, headers, payload, requestData) {
84 if (status === 200 && requestData.method === 'POST') {
85 let accessToken = payload.token;
86
87 if (isPresent (accessToken)) {
88 // Delete the access token from the payload.
89 delete payload.token;
90
91 // The account was created and logged in at the same time. We need to
92 // extract the token, and register it with the gatekeeper service.
93 let session = this.get ('session');
94 let currentUser = {id: payload.account._id, username: payload.account.username, email: payload.account.email};
95
96 session.setProperties ({currentUser, accessToken});
97 }
98 }
99
100 return this._super (status, headers, payload, requestData);
101 }
102});
103
104//From http://stackoverflow.com/questions/280634/endswith-in-javascript
105function endsWith (string, suffix) {
106 if (typeof String.prototype.endsWith !== 'function') {
107 return string.indexOf(suffix, string.length - suffix.length) !== -1;
108 } else {
109 return string.endsWith(suffix);
110 }
111}
\No newline at end of file