ember-simple-auth icon indicating copy to clipboard operation
ember-simple-auth copied to clipboard

Retry transitions after page reloads

Open Windvis opened this issue 1 year ago • 1 comments

ember-simple-auth supports retrying transitions after the session is authenticated, but this only works if the page isn't refreshed. session.attemptedTransition isn't persisted, so the info is gone after a reload.

ember-simple-auth does already support a cookie-based retry, but that is only enabled during FastBoot. Would it be an option to either always use this cookie-based setup, or make it an opt-in somehow?

Our use-case We have an authenticator that redirects users to an authentication server on a different domain (which then redirects back to the app with a token), but as a result the original transition information is lost.

We can hook into the cookie-based redirect setup by writing the url to the ember_simple_auth-redirectTarget cookie ourselves (in the overriden requireAuthentication method of our app's SessionService). But it would be nice if this wouldn't require custom app code.

Code of our workaround
export default class SessionService extends BaseSessionService {
  @service router;

  requireAuthentication(transition, routeOrCallback) {
    const isAuthenticated = super.requireAuthentication(
      transition,
      routeOrCallback
    );

    if (!isAuthenticated) {
      const COOKIE_NAME = 'ember_simple_auth-redirectTarget';
      const redirectUrl = routeInfoUrl(transition.to, this.router); // This is a util that reimplements `transition.intent.url` with public APIs
      document.cookie = `${COOKIE_NAME}=${redirectUrl};path=/;samesite=strict`;
    }

    return isAuthenticated;
  }

Windvis avatar Jul 30 '24 09:07 Windvis