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

Simplify session/localStorage logic; Add isLoggedIn check #126

Merged
merged 4 commits into from
Apr 6, 2023
Merged
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
76 changes: 29 additions & 47 deletions web/app/routes/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,40 @@ export default class AuthenticatedRoute extends Route {
@service declare session: SessionService;
@service declare authenticatedUser: AuthenticatedUserService;

async afterModel(): Promise<void> {
await this.authenticatedUser.loadInfo.perform();
void this.session.pollForExpiredAuth.perform();
}

async beforeModel(transition: any): Promise<void> {
async afterModel(transition: any): Promise<void> {
// If the user isn't authenticated, transition to the auth screen
let requireAuthentication = this.session.requireAuthentication(
let isLoggedIn = this.session.requireAuthentication(
transition,
"authenticate"
);

// See if we have a redirect target stored in sessionStorage
let storageItem = window.sessionStorage.getItem(REDIRECT_STORAGE_KEY);

if (!storageItem) {
// If we don't have a redirect target in sessionStorage, check localStorage
storageItem = window.localStorage.getItem(REDIRECT_STORAGE_KEY);

// If the redirect target in localStorage is expired, remove it
if (storageItem && Date.now() > JSON.parse(storageItem).expiresOn) {
window.localStorage.removeItem(REDIRECT_STORAGE_KEY);
storageItem = null;
}
if (isLoggedIn) {
await this.authenticatedUser.loadInfo.perform();
void this.session.pollForExpiredAuth.perform();
}
}

if (
!storageItem &&
!requireAuthentication &&
transition.to.name != "authenticated.index"
) {
// 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

/**
* 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;

window.sessionStorage.setItem(REDIRECT_STORAGE_KEY, transitionTo);
window.localStorage.setItem(
REDIRECT_STORAGE_KEY,
JSON.stringify({
url: transitionTo,
expiresOn: Date.now() + 60 * 5000, // 5 minutes
})
);
}
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
})
);
}
}