Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.0 #26

Merged
merged 8 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion addon/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions addon/authenticators/fleetbase.js
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
20 changes: 17 additions & 3 deletions addon/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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...'));
}
}

Expand All @@ -124,7 +125,7 @@ export default class SessionService extends SimpleAuthSessionService {
transition.abort();
}

reject(invalidateWithLoader(`Session authentication failed...`));
reject(invalidateWithLoader('Session authentication failed...'));
}

resolve(user);
Expand Down Expand Up @@ -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);
});
}
}
28 changes: 0 additions & 28 deletions addon/utils/extract-latitude.js

This file was deleted.

28 changes: 0 additions & 28 deletions addon/utils/extract-longitude.js

This file was deleted.

1 change: 0 additions & 1 deletion app/utils/extract-latitude.js

This file was deleted.

1 change: 0 additions & 1 deletion app/utils/extract-longitude.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
10 changes: 0 additions & 10 deletions tests/unit/utils/extract-latitude-test.js

This file was deleted.

10 changes: 0 additions & 10 deletions tests/unit/utils/extract-longitude-test.js

This file was deleted.

Loading