forked from hashicorp-forge/hermes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from meirish/b-post-auth-redirect
Save target url in SessionStorage for post-auth redirect; Add EmberSimpleAuth types
- Loading branch information
Showing
7 changed files
with
80 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
import Route from "@ember/routing/route"; | ||
import { inject as service } from "@ember/service"; | ||
import ConfigService from "hermes/services/config"; | ||
import AuthenticateController from "hermes/controllers/authenticate"; | ||
import AuthenticatedUserService from "hermes/services/authenticated-user"; | ||
import Transition from "@ember/routing/transition"; | ||
import window from "ember-window-mock"; | ||
import SessionService from "hermes/services/session"; | ||
|
||
export default class AuthenticatedRoute extends Route { | ||
@service declare session: any; | ||
@service declare session: SessionService; | ||
@service declare authenticatedUser: AuthenticatedUserService; | ||
@service("config") declare configSvc: ConfigService; | ||
|
||
async afterModel(): Promise<void> { | ||
// Load user info | ||
await this.authenticatedUser.loadInfo.perform(); | ||
} | ||
|
||
async beforeModel(transition: Transition): Promise<void> { | ||
// Check if the request requires authentication and if so, preserve the URL | ||
async beforeModel(transition: any): Promise<void> { | ||
// If the user isn't authenticated, transition to the auth screen | ||
let requireAuthentication = this.session.requireAuthentication( | ||
transition, | ||
"authenticate" | ||
); | ||
|
||
if (!requireAuthentication && transition.to.name != "authenticated.index") { | ||
let authenticateController = this.controllerFor( | ||
"authenticate" | ||
) as AuthenticateController; | ||
let target = window.sessionStorage.getItem( | ||
this.session.SESSION_STORAGE_KEY | ||
); | ||
if ( | ||
!target && | ||
!requireAuthentication && | ||
transition.to.name != "authenticated" | ||
) { | ||
// 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 | ||
|
||
// Set previous transition to preserve URL | ||
authenticateController.previousTransition = transition; | ||
window.sessionStorage.setItem( | ||
this.session.SESSION_STORAGE_KEY, | ||
transition.intent.url | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { inject as service } from "@ember/service"; | ||
import RouterService from "@ember/routing/router-service"; | ||
import EmberSimpleAuthSessionService from "ember-simple-auth/services/session"; | ||
import window from "ember-window-mock"; | ||
|
||
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 transition; | ||
|
||
if (redirectTarget) { | ||
transition = this.router.transitionTo(redirectTarget); | ||
} else { | ||
transition = this.router.transitionTo(routeAfterAuthentication); | ||
} | ||
transition.followRedirects().then(() => { | ||
window.sessionStorage.removeItem(this.SESSION_STORAGE_KEY); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Reference: https://github.com/mainmatter/ember-simple-auth/blob/master/packages/ember-simple-auth/addon/services/session.js | ||
|
||
import Service from "@ember/service"; | ||
import Evented from "@ember/object/evented"; | ||
import Transition from "@ember/routing/transition"; | ||
|
||
export interface Data { | ||
authenticated: { | ||
access_token: string; | ||
}; | ||
} | ||
|
||
declare module "ember-simple-auth/services/session" { | ||
export default class EmberSimpleAuthSessionService extends Service.extend(Evented) { | ||
data: Data; | ||
setup: () => void; | ||
authenticate(...args: any[]): RSVP.Promise; | ||
invalidate(...args: any): RSVP.Promise; | ||
requireAuthentication( | ||
transition: Transition, | ||
routeOrCallback: string | function | ||
): RSVP.Promise; | ||
prohibitAuthentication(routeOrCallback: string | function): RSVP.Promise; | ||
} | ||
} |