API Docs for: 0.25.0
Show:

File: addon/mixins/cas-authenticated-route.js

  1. import Ember from 'ember';
  2. import { getAuthUrl } from 'ember-osf/utils/auth';
  3.  
  4. /**
  5. * @module ember-osf
  6. * @submodule mixins
  7. */
  8.  
  9. /**
  10. * Replacement for Ember-simple-auth AuthenticatedRouteMixin. Instead of redirecting to an internal route,
  11. * this mixin redirects to CAS login URL, and brings the user back to the last requested page afterwards
  12. *
  13. * For OAuth this is done via the state parameter, and for cookies this is done via the service parameter. (TODO: Need a mixin that detects this!)
  14. *
  15. * @class CasAuthenticatedRouteMixin
  16. */
  17. export default Ember.Mixin.create({
  18. /**
  19. The session service.
  20. @property session
  21. @readOnly
  22. @type SessionService
  23. @public
  24. */
  25. session: Ember.inject.service('session'),
  26. routing: Ember.inject.service('-routing'),
  27.  
  28. /**
  29. Checks whether the session is authenticated, and if it is not, attempts to authenticate it, and if that fails,
  30. redirects to the login URL. (Sending back to this page after a successful transition)
  31.  
  32. __If `beforeModel` is overridden in a route that uses this mixin, the route's
  33. implementation must call `this._super(...arguments)`__ so that the mixin's
  34. `beforeModel` method is actually executed.
  35. @method beforeModel
  36. @public
  37. */
  38. beforeModel(transition) {
  39. if (this.get('session.isAuthenticated')) {return this._super(...arguments)}
  40. return this.get('session').authenticate('authenticator:osf-cookie').then(() => {
  41. return this._super(...arguments);
  42. }).catch(() => {
  43. // Reference: http://stackoverflow.com/a/39054607/414097
  44. let routing = this.get('routing');
  45. let params = Object.values(transition.params).filter(param => Object.values(param).length);
  46. let url = routing.generateURL(transition.targetName, params, transition.queryParams);
  47. window.location = getAuthUrl(window.location.origin + url);
  48. })
  49. }
  50. });
  51.