diff --git a/addon/adapters/application.js b/addon/adapters/application.js index 80cc93f..352ddf1 100644 --- a/addon/adapters/application.js +++ b/addon/adapters/application.js @@ -95,7 +95,9 @@ export default class ApplicationAdapter extends RESTAdapter { const localStorageSession = JSON.parse(window.localStorage.getItem('ember_simple_auth-session')); if (localStorageSession) { const { authenticated } = localStorageSession; - token = authenticated.token; + if (authenticated) { + token = authenticated.token; + } // Check isAuthenticated again isAuthenticated = !!token; diff --git a/addon/authenticators/fleetbase.js b/addon/authenticators/fleetbase.js index 4b8a50b..d29afc9 100644 --- a/addon/authenticators/fleetbase.js +++ b/addon/authenticators/fleetbase.js @@ -1,5 +1,17 @@ import Base from 'ember-simple-auth/authenticators/base'; import { inject as service } from '@ember/service'; +import getWithDefault from '../utils/get-with-default'; + +export class AuthenticationError extends Error { + constructor(message, code) { + super(message); + this.code = code; + } + + getCode() { + return this.code; + } +} export default class FleetbaseAuthenticator extends Base { /** @@ -35,7 +47,7 @@ export default class FleetbaseAuthenticator extends Base { ) .then((response) => { if (response.restore === false) { - return Promise.reject(new Error(response.error)); + return Promise.reject(new AuthenticationError(response.error)); } return response; @@ -52,7 +64,10 @@ export default class FleetbaseAuthenticator extends Base { authenticate(credentials = {}, remember = false, path = 'auth/login') { return this.fetch.post(path, { ...credentials, remember }).then((response) => { if (response.errors) { - return Promise.reject(new Error(response.errors.firstObject ?? 'Authentication failed!')); + const errorMessage = getWithDefault(response.errors, '0', 'Authentication failed!'); + const errorCode = getWithDefault(response, 'code'); + + return Promise.reject(new AuthenticationError(errorMessage, errorCode)); } return response; diff --git a/addon/services/session.js b/addon/services/session.js index 4cda6dd..ea8f061 100644 --- a/addon/services/session.js +++ b/addon/services/session.js @@ -2,6 +2,7 @@ import SimpleAuthSessionService from 'ember-simple-auth/services/session'; import { tracked } from '@glimmer/tracking'; import { inject as service } from '@ember/service'; import { later } from '@ember/runloop'; +import getWithDefault from '../utils/get-with-default'; export default class SessionService extends SimpleAuthSessionService { /** @@ -97,12 +98,12 @@ export default class SessionService extends SimpleAuthSessionService { const user = await this.currentUser.load(); if (!user) { - return this.invalidateWithLoader(`Session authentication failed...`); + return this.invalidateWithLoader('Session authentication failed...'); } return user; } catch (error) { - await this.invalidateWithLoader(error.message ?? `Session authentication failed...`); + await this.invalidateWithLoader(getWithDefault(error, 'message', 'Session authentication failed...')); } } @@ -124,7 +125,7 @@ export default class SessionService extends SimpleAuthSessionService { transition.abort(); } - reject(invalidateWithLoader(`Session authentication failed...`)); + reject(invalidateWithLoader('Session authentication failed...')); } resolve(user); @@ -218,4 +219,17 @@ export default class SessionService extends SimpleAuthSessionService { return Math.round((now - date) / 1000); } + + /** + * Checks for the presence of two-factor authentication for a given user identity. + * + * @param {String} identity + * @return {Promise} + * @throws {Error} + */ + checkForTwoFactor(identity) { + return this.fetch.get('two-fa/check', { identity }).catch((error) => { + throw new Error(error.message); + }); + } } diff --git a/addon/utils/extract-latitude.js b/addon/utils/extract-latitude.js deleted file mode 100644 index a71f121..0000000 --- a/addon/utils/extract-latitude.js +++ /dev/null @@ -1,28 +0,0 @@ -/* eslint-disable no-unused-vars */ -import extractCoordinates from './extract-coordinates'; -import Terraformer from 'terraformer'; -import { isArray } from '@ember/array'; - -export default function extractLatitude(position) { - let latitude, longitude; - - if (!position) { - return 0; - } - - if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [latitude, longitude] = extractCoordinates(position.coordinates); - - return latitude; - } - - if (typeof position === 'object') { - let latitude = position['lat'] || position['latitude'] || position['x']; - - return latitude; - } - - [latitude, longitude] = extractCoordinates(position); - - return latitude; -} diff --git a/addon/utils/extract-longitude.js b/addon/utils/extract-longitude.js deleted file mode 100644 index bc2f4be..0000000 --- a/addon/utils/extract-longitude.js +++ /dev/null @@ -1,28 +0,0 @@ -/* eslint-disable no-unused-vars */ -import extractCoordinates from './extract-coordinates'; -import Terraformer from 'terraformer'; -import { isArray } from '@ember/array'; - -export default function extractLongitude(position) { - let latitude, longitude; - - if (!position) { - return 0; - } - - if (position instanceof Terraformer.Point || isArray(position.coordinates)) { - [latitude, longitude] = extractCoordinates(position.coordinates); - - return longitude; - } - - if (typeof position === 'object') { - let longitude = position['lng'] || position['longitude'] || position['lon'] || position['y']; - - return longitude; - } - - [latitude, longitude] = extractCoordinates(position); - - return longitude; -} diff --git a/app/utils/extract-latitude.js b/app/utils/extract-latitude.js deleted file mode 100644 index 9f0fda8..0000000 --- a/app/utils/extract-latitude.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from '@fleetbase/ember-core/utils/extract-latitude'; diff --git a/app/utils/extract-longitude.js b/app/utils/extract-longitude.js deleted file mode 100644 index 397b5a0..0000000 --- a/app/utils/extract-longitude.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from '@fleetbase/ember-core/utils/extract-longitude'; diff --git a/package.json b/package.json index 6d08c24..4992e1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/ember-core", - "version": "0.1.9", + "version": "0.2.0", "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.", "keywords": [ "fleetbase-core", diff --git a/tests/unit/utils/extract-latitude-test.js b/tests/unit/utils/extract-latitude-test.js deleted file mode 100644 index 1529d50..0000000 --- a/tests/unit/utils/extract-latitude-test.js +++ /dev/null @@ -1,10 +0,0 @@ -import extractLatitude from 'dummy/utils/extract-latitude'; -import { module, test } from 'qunit'; - -module('Unit | Utility | extract-latitude', function () { - // TODO: Replace this with your real tests. - test('it works', function (assert) { - let result = extractLatitude(); - assert.ok(result); - }); -}); diff --git a/tests/unit/utils/extract-longitude-test.js b/tests/unit/utils/extract-longitude-test.js deleted file mode 100644 index 60670ed..0000000 --- a/tests/unit/utils/extract-longitude-test.js +++ /dev/null @@ -1,10 +0,0 @@ -import extractLongitude from 'dummy/utils/extract-longitude'; -import { module, test } from 'qunit'; - -module('Unit | Utility | extract-longitude', function () { - // TODO: Replace this with your real tests. - test('it works', function (assert) { - let result = extractLongitude(); - assert.ok(result); - }); -});