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

Separate front- and back-end redirect handling #128

Merged
merged 15 commits into from
Apr 7, 2023
2 changes: 2 additions & 0 deletions web/app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export default class ApplicationRoute extends Route {
@service("fetch") fetchSvc;
@service flags;
@service session;
@service router;

@action
error(error) {
if (error instanceof UnauthorizedError) {
this.session.invalidate();
this.router.transitionTo("authenticate");
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions web/app/routes/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export default class AuthenticateRoute extends Route {
@service declare session: SessionService;

beforeModel() {
/**
* Checks if the session is authenticated,
* and if it is, transitions to the specified route
*/
this.session.prohibitAuthentication("/");
}
}
33 changes: 23 additions & 10 deletions web/app/routes/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@ export default class AuthenticatedRoute extends Route {
@service declare session: SessionService;
@service declare authenticatedUser: AuthenticatedUserService;

async beforeModel(transition: any): Promise<void> {
// If the user isn't authenticated, transition to the auth screen
let isLoggedIn = this.session.requireAuthentication(
transition,
"authenticate"
);
if (isLoggedIn) {
await this.authenticatedUser.loadInfo.perform();
void this.session.pollForExpiredAuth.perform();
}
async beforeModel(transition: any) {
/**
* Checks if the session is authenticated in the front end.
* If unauthenticated, it will redirect to the auth screen
*/
this.session.requireAuthentication(transition, "authenticate");
}

// Note: Only called if the session is authenticated in the front end
async afterModel() {
/**
* Checks if the session is authenticated in the back end.
* If the `loadInfo` task returns a 401, it will bubble up to the
* application error method which invalidates the session
* and redirects to the auth screen.
*/
await this.authenticatedUser.loadInfo.perform();

/**
* If the session is authenticated with the front- and back-ends,
* kick off the task to poll for expired auth.
*/
void this.session.pollForExpiredAuth.perform();
}
}