Skip to content

Commit

Permalink
improvements and fixes to core services
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Sep 10, 2024
1 parent 6126265 commit 9d02322
Show file tree
Hide file tree
Showing 6 changed files with 610 additions and 197 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 All @@ -28,7 +29,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @return {SessionService}
*/
isOnboarding() {
isOnboarding () {
this._isOnboarding = true;

return this;
Expand All @@ -37,7 +38,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 @@ -46,36 +47,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 @@ -95,7 +100,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 @@ -114,6 +119,7 @@ export default class SessionService extends SimpleAuthSessionService {
if (transition) {
transition.abort();
}

await invalidateWithLoader(error.message ?? 'Session authentication failed...');
throw error;
}
Expand All @@ -125,7 +131,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 @@ -147,15 +153,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 @@ -174,7 +179,7 @@ export default class SessionService extends SimpleAuthSessionService {
*
* @void
*/
setRedirect(whereTo = 'console') {
setRedirect (whereTo = 'console') {
this.redirectTo = whereTo;
}

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

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

Expand All @@ -206,8 +211,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 9d02322

Please sign in to comment.