1 | import RESTAdapter from '../-lib/user/adapters/rest';
|
2 |
|
3 | import { isPresent, isEmpty } from '@ember/utils';
|
4 | import { getWithDefault } from '@ember/object';
|
5 | import { readOnly } from '@ember/object/computed';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export default RESTAdapter.extend({
|
13 |
|
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 |
|
31 |
|
32 |
|
33 |
|
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 ) {
|
66 | let hash = this._super (...arguments);
|
67 | let beforeSend = hash.beforeSend || function (/*xhr*/) { };
|
68 |
|
69 | hash.beforeSend = function (xhr) {
|
70 |
|
71 | beforeSend (xhr);
|
72 |
|
73 | if (endsWith (url, '/me')) {
|
74 |
|
75 |
|
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 |
|
89 | delete payload.token;
|
90 |
|
91 |
|
92 |
|
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 |
|
105 | function 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 |