@fleetbase/ember-core 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -95,7 +95,9 @@ export default class ApplicationAdapter extends RESTAdapter {
95
95
  const localStorageSession = JSON.parse(window.localStorage.getItem('ember_simple_auth-session'));
96
96
  if (localStorageSession) {
97
97
  const { authenticated } = localStorageSession;
98
- token = authenticated.token;
98
+ if (authenticated) {
99
+ token = authenticated.token;
100
+ }
99
101
 
100
102
  // Check isAuthenticated again
101
103
  isAuthenticated = !!token;
@@ -1,5 +1,17 @@
1
1
  import Base from 'ember-simple-auth/authenticators/base';
2
2
  import { inject as service } from '@ember/service';
3
+ import getWithDefault from '../utils/get-with-default';
4
+
5
+ export class AuthenticationError extends Error {
6
+ constructor(message, code) {
7
+ super(message);
8
+ this.code = code;
9
+ }
10
+
11
+ getCode() {
12
+ return this.code;
13
+ }
14
+ }
3
15
 
4
16
  export default class FleetbaseAuthenticator extends Base {
5
17
  /**
@@ -35,7 +47,7 @@ export default class FleetbaseAuthenticator extends Base {
35
47
  )
36
48
  .then((response) => {
37
49
  if (response.restore === false) {
38
- return Promise.reject(new Error(response.error));
50
+ return Promise.reject(new AuthenticationError(response.error));
39
51
  }
40
52
 
41
53
  return response;
@@ -52,7 +64,10 @@ export default class FleetbaseAuthenticator extends Base {
52
64
  authenticate(credentials = {}, remember = false, path = 'auth/login') {
53
65
  return this.fetch.post(path, { ...credentials, remember }).then((response) => {
54
66
  if (response.errors) {
55
- return Promise.reject(new Error(response.errors.firstObject ?? 'Authentication failed!'));
67
+ const errorMessage = getWithDefault(response.errors, '0', 'Authentication failed!');
68
+ const errorCode = getWithDefault(response, 'code');
69
+
70
+ return Promise.reject(new AuthenticationError(errorMessage, errorCode));
56
71
  }
57
72
 
58
73
  return response;
@@ -2,6 +2,7 @@ import SimpleAuthSessionService from 'ember-simple-auth/services/session';
2
2
  import { tracked } from '@glimmer/tracking';
3
3
  import { inject as service } from '@ember/service';
4
4
  import { later } from '@ember/runloop';
5
+ import getWithDefault from '../utils/get-with-default';
5
6
 
6
7
  export default class SessionService extends SimpleAuthSessionService {
7
8
  /**
@@ -97,12 +98,12 @@ export default class SessionService extends SimpleAuthSessionService {
97
98
  const user = await this.currentUser.load();
98
99
 
99
100
  if (!user) {
100
- return this.invalidateWithLoader(`Session authentication failed...`);
101
+ return this.invalidateWithLoader('Session authentication failed...');
101
102
  }
102
103
 
103
104
  return user;
104
105
  } catch (error) {
105
- await this.invalidateWithLoader(error.message ?? `Session authentication failed...`);
106
+ await this.invalidateWithLoader(getWithDefault(error, 'message', 'Session authentication failed...'));
106
107
  }
107
108
  }
108
109
 
@@ -124,7 +125,7 @@ export default class SessionService extends SimpleAuthSessionService {
124
125
  transition.abort();
125
126
  }
126
127
 
127
- reject(invalidateWithLoader(`Session authentication failed...`));
128
+ reject(invalidateWithLoader('Session authentication failed...'));
128
129
  }
129
130
 
130
131
  resolve(user);
@@ -218,4 +219,17 @@ export default class SessionService extends SimpleAuthSessionService {
218
219
 
219
220
  return Math.round((now - date) / 1000);
220
221
  }
222
+
223
+ /**
224
+ * Checks for the presence of two-factor authentication for a given user identity.
225
+ *
226
+ * @param {String} identity
227
+ * @return {Promise}
228
+ * @throws {Error}
229
+ */
230
+ checkForTwoFactor(identity) {
231
+ return this.fetch.get('two-fa/check', { identity }).catch((error) => {
232
+ throw new Error(error.message);
233
+ });
234
+ }
221
235
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/ember-core",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
5
5
  "keywords": [
6
6
  "fleetbase-core",
@@ -1,28 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- import extractCoordinates from './extract-coordinates';
3
- import Terraformer from 'terraformer';
4
- import { isArray } from '@ember/array';
5
-
6
- export default function extractLatitude(position) {
7
- let latitude, longitude;
8
-
9
- if (!position) {
10
- return 0;
11
- }
12
-
13
- if (position instanceof Terraformer.Point || isArray(position.coordinates)) {
14
- [latitude, longitude] = extractCoordinates(position.coordinates);
15
-
16
- return latitude;
17
- }
18
-
19
- if (typeof position === 'object') {
20
- let latitude = position['lat'] || position['latitude'] || position['x'];
21
-
22
- return latitude;
23
- }
24
-
25
- [latitude, longitude] = extractCoordinates(position);
26
-
27
- return latitude;
28
- }
@@ -1,28 +0,0 @@
1
- /* eslint-disable no-unused-vars */
2
- import extractCoordinates from './extract-coordinates';
3
- import Terraformer from 'terraformer';
4
- import { isArray } from '@ember/array';
5
-
6
- export default function extractLongitude(position) {
7
- let latitude, longitude;
8
-
9
- if (!position) {
10
- return 0;
11
- }
12
-
13
- if (position instanceof Terraformer.Point || isArray(position.coordinates)) {
14
- [latitude, longitude] = extractCoordinates(position.coordinates);
15
-
16
- return longitude;
17
- }
18
-
19
- if (typeof position === 'object') {
20
- let longitude = position['lng'] || position['longitude'] || position['lon'] || position['y'];
21
-
22
- return longitude;
23
- }
24
-
25
- [latitude, longitude] = extractCoordinates(position);
26
-
27
- return longitude;
28
- }
@@ -1 +0,0 @@
1
- export { default } from '@fleetbase/ember-core/utils/extract-latitude';
@@ -1 +0,0 @@
1
- export { default } from '@fleetbase/ember-core/utils/extract-longitude';