Skip to content

Commit

Permalink
Merge pull request #52 from fleetbase/dev-v0.2.18-additionals
Browse files Browse the repository at this point in the history
improvements and fixes to core services
  • Loading branch information
roncodes authored Sep 10, 2024
2 parents 61e5d50 + 9d02322 commit fa1e1d4
Show file tree
Hide file tree
Showing 5 changed files with 609 additions and 196 deletions.
45 changes: 25 additions & 20 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 { debug } from '@ember/debug';
import getWithDefault from '../utils/get-with-default';

export default class SessionService extends SimpleAuthSessionService {
Expand Down Expand Up @@ -29,7 +30,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @return {SessionService}
*/
isOnboarding() {
isOnboarding () {
this._isOnboarding = true;

return this;
Expand All @@ -38,7 +39,7 @@ export default class SessionService extends SimpleAuthSessionService {
/**
* Manually authenticate user
*/
manuallyAuthenticate(authToken) {
manuallyAuthenticate (authToken) {
return this.session._setup('authenticator:fleetbase', { token: authToken }, true);
}

Expand All @@ -47,36 +48,40 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @void
*/
async handleAuthentication() {
async handleAuthentication () {
if (this._isOnboarding) {
return;
}

const loaderNode = this.showLoader('Starting session...');
this.isLoaderNodeOpen = true;

try {
await this.router.transitionTo(this.redirectTo);
const removeLoaderNode = () => {
later(
this,
() => {
// remove node from body
document.body.removeChild(loaderNode);
this.isLoaderNodeOpen = false;
},
600 * 6
600 * 3
);
};
this.isLoaderNodeOpen = true;

try {
await this.router.transitionTo(this.redirectTo);
} catch (error) {
this.notifications.serverError(error);
debug(`Session's handleAuthentication() failed to transition: ${error.message}`);
}

removeLoaderNode();
}

/**
* Loads the current authenticated user
*
* @void
*/
async loadCurrentUser() {
async loadCurrentUser () {
try {
const user = await this.currentUser.load();

Expand All @@ -96,7 +101,7 @@ export default class SessionService extends SimpleAuthSessionService {
* @param {Transition} transition
* @void
*/
async promiseCurrentUser(transition = null) {
async promiseCurrentUser (transition = null) {
const invalidateWithLoader = this.invalidateWithLoader.bind(this);

try {
Expand All @@ -115,6 +120,7 @@ export default class SessionService extends SimpleAuthSessionService {
if (transition) {
transition.abort();
}

await invalidateWithLoader(error.message ?? 'Session authentication failed...');
throw error;
}
Expand All @@ -126,7 +132,7 @@ export default class SessionService extends SimpleAuthSessionService {
* @param {String} loadingMessage
* @return {HTMLElement} loader
*/
showLoader(loadingMessage) {
showLoader (loadingMessage) {
const loader = document.createElement('div');
loader.classList.add('overloader');
loader.innerHTML = `<div class="flex items-center justify-center">
Expand All @@ -148,15 +154,14 @@ export default class SessionService extends SimpleAuthSessionService {
* @param {String} loadingMessage
* @return {Promise}
*/
invalidateWithLoader(loadingMessage = 'Ending session...') {
invalidateWithLoader (loadingMessage = 'Ending session...') {
// if loader node is open already just invalidate
if (this.isLoaderNodeOpen === true) {
return this.session.invalidate();
}

const loaderNode = this.showLoader(loadingMessage);

this.isLoaderNodeOpen = false;
this.isLoaderNodeOpen = true;

return this.session.invalidate().then(() => {
later(
Expand All @@ -175,7 +180,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @void
*/
setRedirect(whereTo = 'console') {
setRedirect (whereTo = 'console') {
this.redirectTo = whereTo;
}

Expand All @@ -184,7 +189,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @return {Date}
*/
getExpiresAtDate() {
getExpiresAtDate () {
return new Date(this.data.authenticated.expires_at);
}

Expand All @@ -193,7 +198,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @return {Integer}
*/
getSessionSecondsRemaining() {
getSessionSecondsRemaining () {
const date = this.getExpiresAtDate();
const now = new Date();

Expand All @@ -207,8 +212,8 @@ export default class SessionService extends SimpleAuthSessionService {
* @return {Promise}
* @throws {Error}
*/
checkForTwoFactor(identity) {
return this.fetch.get('two-fa/check', { identity }).catch((error) => {
checkForTwoFactor (identity) {
return this.fetch.get('two-fa/check', { identity }).catch(error => {
throw new Error(error.message);
});
}
Expand Down
2 changes: 1 addition & 1 deletion addon/services/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class ThemeService extends Service {
*/
get router() {
const owner = getOwner(this);
const router = owner.lookup('service:router');
const router = owner.lookup('router:main');

return router;
}
Expand Down
Loading

0 comments on commit fa1e1d4

Please sign in to comment.