Skip to content

Commit

Permalink
Move SESSION_STORAGE_KEY out of SessionService (hashicorp-forge#84)
Browse files Browse the repository at this point in the history
Move variable out of class
  • Loading branch information
jeffdaley authored Mar 14, 2023
1 parent 535f341 commit ec1761f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
11 changes: 3 additions & 8 deletions web/app/routes/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 from "hermes/services/session";
import SessionService, { SESSION_STORAGE_KEY } from "hermes/services/session";

export default class AuthenticatedRoute extends Route {
@service declare session: SessionService;
Expand All @@ -19,9 +19,7 @@ export default class AuthenticatedRoute extends Route {
"authenticate"
);

let target = window.sessionStorage.getItem(
this.session.SESSION_STORAGE_KEY
);
let target = window.sessionStorage.getItem(SESSION_STORAGE_KEY);

if (
!target &&
Expand All @@ -30,10 +28,7 @@ export default class AuthenticatedRoute extends Route {
) {
// ember-simple-auth uses this value to set cookies when fastboot is enabled: https://github.com/mainmatter/ember-simple-auth/blob/a7e583cf4d04d6ebc96b198a8fa6dde7445abf0e/packages/ember-simple-auth/addon/-internals/routing.js#L12

window.sessionStorage.setItem(
this.session.SESSION_STORAGE_KEY,
transition.intent.url
);
window.sessionStorage.setItem(SESSION_STORAGE_KEY, transition.intent.url);
}
}
}
10 changes: 4 additions & 6 deletions web/app/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import RouterService from "@ember/routing/router-service";
import EmberSimpleAuthSessionService from "ember-simple-auth/services/session";
import window from "ember-window-mock";

export const SESSION_STORAGE_KEY = "hermes.redirectTarget";

export default class SessionService extends EmberSimpleAuthSessionService {
@service declare router: RouterService;

readonly SESSION_STORAGE_KEY: string = "hermes.redirectTarget";

// ember-simple-auth only uses a cookie to track redirect target if you're using fastboot, otherwise it keeps track of the redirect target as a parameter on the session service. See the source here: https://github.com/mainmatter/ember-simple-auth/blob/a7e583cf4d04d6ebc96b198a8fa6dde7445abf0e/packages/ember-simple-auth/addon/-internals/routing.js#L33-L50
//
// Because we redirect as part of the authentication flow, the parameter storing the transition gets reset. Instead, we keep track of the redirectTarget in browser sessionStorage and override the handleAuthentication method as recommended by ember-simple-auth.

handleAuthentication(routeAfterAuthentication: string) {
let redirectTarget = window.sessionStorage.getItem(
this.SESSION_STORAGE_KEY
);
let redirectTarget = window.sessionStorage.getItem(SESSION_STORAGE_KEY);
let transition;

if (redirectTarget) {
Expand All @@ -26,7 +24,7 @@ export default class SessionService extends EmberSimpleAuthSessionService {
);
}
transition.followRedirects().then(() => {
window.sessionStorage.removeItem(this.SESSION_STORAGE_KEY);
window.sessionStorage.removeItem(SESSION_STORAGE_KEY);
});
}
}

0 comments on commit ec1761f

Please sign in to comment.