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

Move SESSION_STORAGE_KEY out of SessionService #84

Merged
merged 1 commit into from
Mar 14, 2023
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
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);
});
}
}