Skip to content

Commit

Permalink
Move storage call to Application route (#127)
Browse files Browse the repository at this point in the history
* Simplify session/localStorage logic; add isLoggedIn check

* Revert application.js change

* Add comment

* Reduce diff

* Move redirect storage call into Application route
  • Loading branch information
jeffdaley committed Apr 7, 2023
1 parent 12f1f30 commit a303a44
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
27 changes: 26 additions & 1 deletion web/app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { UnauthorizedError } from "@ember-data/adapter/error";
import { action } from "@ember/object";
import config from "hermes/config/environment";
import { inject as service } from "@ember/service";
import window from "ember-window-mock";
import { REDIRECT_STORAGE_KEY } from "hermes/services/session";

export default class ApplicationRoute extends Route {
@service config;
Expand All @@ -18,7 +20,30 @@ export default class ApplicationRoute extends Route {
}
}

async beforeModel() {
async beforeModel(transition) {
/**
* We expect a `transition.intent.url`, but in rare cases, it's undefined,
* e.g., when clicking the "view dashboard" button from the 404 route.
* When this happens, we fall back to `transition.to.name`.
*
* For reference:
* `transition.intent.url` e.g., 'documents/1'
* `transition.to.name` e.g., 'authenticated.documents'
*/
let transitionTo = transition.intent.url ?? transition.to.name;

/**
* Capture the transition intent and save it to session/localStorage.
*/
window.sessionStorage.setItem(REDIRECT_STORAGE_KEY, transitionTo);
window.localStorage.setItem(
REDIRECT_STORAGE_KEY,
JSON.stringify({
url: transitionTo,
expiresOn: Date.now() + 60 * 5000, // 5 minutes
})
);

await this.session.setup();

// Flags read from the environment and set properties on the service this
Expand Down
30 changes: 2 additions & 28 deletions web/app/routes/authenticated.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";
import AuthenticatedUserService from "hermes/services/authenticated-user";
import window from "ember-window-mock";
import SessionService, { REDIRECT_STORAGE_KEY } from "hermes/services/session";
import SessionService from "hermes/services/session";

export default class AuthenticatedRoute extends Route {
@service declare session: SessionService;
@service declare authenticatedUser: AuthenticatedUserService;

async afterModel(transition: any): Promise<void> {
async beforeModel(transition: any): Promise<void> {
// If the user isn't authenticated, transition to the auth screen
let isLoggedIn = this.session.requireAuthentication(
transition,
Expand All @@ -19,29 +18,4 @@ export default class AuthenticatedRoute extends Route {
void this.session.pollForExpiredAuth.perform();
}
}

beforeModel(transition: any): void {
/**
* We expect a `transition.intent.url`, but in rare cases, it's undefined,
* e.g., when clicking the "view dashboard" button from the 404 route.
* When this happens, we fall back to `transition.to.name`.
*
* For reference:
* `transition.intent.url` e.g., 'documents/1'
* `transition.to.name` e.g., 'authenticated.documents'
*/
let transitionTo = transition.intent.url ?? transition.to.name;

/**
* Capture the transition intent and save it to session/localStorage.
*/
window.sessionStorage.setItem(REDIRECT_STORAGE_KEY, transitionTo);
window.localStorage.setItem(
REDIRECT_STORAGE_KEY,
JSON.stringify({
url: transitionTo,
expiresOn: Date.now() + 60 * 5000, // 5 minutes
})
);
}
}

0 comments on commit a303a44

Please sign in to comment.