Search code examples
javascriptember.jsember-simple-auth

Ember Simple Auth subdomain redirects


I've been able to successfully setup multiple Ember applications with shared Ember Simple Auth cookie based authentication. The next step of my journey is to setup proper redirects between applications. Hear me out.

Scenario A (this works out of the box, yay!)

  • User tries to access domain.com/deep/link/resource
  • User is NOT logged in
  • User is redirected to domain.com/login to login via single sign-on component that uses Ember Simple Auth to save cookie with token
  • After successful login user is redirected back to domain.com/deep/link/resource via previous transition

Scenario B (this works out of the box, yay!)

  • User tries to access app-b.domain.com/deep/link/resource
  • User is already logged in via Ember Simple Auth cookie with token
  • User can access app-b.domain.com/deep/link/resource route

Scenario C (this is what I need to achieve)

  • User tries to access app-a.domain.com/deep/link/resource
  • User is NOT logged in
  • User is redirected to domain.com/login to login via single sign-on component that uses Ember Simple Auth to save cookie with token
  • After successful login user is redirected back to app-a.domain.com/deep/link/resource via previous transition on app-a subdomain

Any help or guidance would be much appreciated. I wonder if I can achieve Scenario C with Ember Simple Auth only, or if I need to write custom redirect logic in beforeModel on subdomains, etc.


Solution

  • You'll be able get scenario C working by overriding the AuthenticatedRouteMixin's beforeModel method. That will by default to an (Ember.js) transition to the login route but in your case you want sth. like window.location.replace('domain.com/login') and remember the current URL in a cookie or so. In order to redirect to app-a.domain.com/deep/link/resource after the user logged in you'll need to override the ApplicationRouteMixin's sessionAuthenticated method so that it redirects to the previous URL remembered in the cookie if that's present and falls back to the default behavior if not.

    Overall, getting this to work should be pretty straight forward actually following these steps.