Skip to content

Instantly share code, notes, and snippets.

@devotox
Last active June 5, 2021 06:36
Show Gist options
  • Save devotox/240c36aa1cb51e63fa2b8917582d2e3f to your computer and use it in GitHub Desktop.
Save devotox/240c36aa1cb51e63fa2b8917582d2e3f to your computer and use it in GitHub Desktop.
Ember Engines With Ember Simple Auth

Using Ember Simple Auth with Ember Engines (In Repo Engine)

  • This shows the steps needed to get Ember Simple Auth to work with Ember Engines as if they are the same application

  • Things to note

    • engine login page needs to transitionToExternal('login')
    • login has to be passed in as an external route to the engine
    • session and cookie services both need to be passed into engine
    • sessionAuthenticated function needs to be overwritten in the Application Controller of the Engine
  • in /app/app.js add to const engines object { [Engine Name]: dependencies }

     	import Ember from 'ember';
     	import Resolver from './resolver';
     	import config from './config/environment';
     	import loadInitializers from 'ember-load-initializers';
    
     	const { Application } = Ember;
     	const { modulePrefix, podModulePrefix } = config;
    
     	const dependencies = {
     		dependencies: {
     			services: ['session', 'cookies'], // You need both session and cookies to be able to route back into the engine from the external login
     			externalRoutes: {
     				'login': 'login' // you can call it whatever you want
     			}
     		}
     	};
    
     	const engines = { // add new engines here
     		[Engine Name]: dependencies,
     	};
    
     	Ember.MODEL_FACTORY_INJECTIONS = true;
    
     	let App = Application.extend({
     		podModulePrefix,
     		modulePrefix,
     		Resolver,
     		engines
     	});
    
     	loadInitializers(App, modulePrefix);
    
     	export default App;
  • In /lib/[Engine Name]/addon/engine.js

     	import Resolver from './resolver';
     	import Engine from 'ember-engines/engine';
     	import config from './config/environment';
     	import loadInitializers from 'ember-load-initializers';
    
     	const { modulePrefix } = config;
    
     	const dependencies = {
     		services: ['session', 'cookies'], // Need both to route into engine after authentication
     		externalRoutes: ['login']
     	};
    
     	const Eng = Engine.extend({
     		dependencies,
     		modulePrefix,
     		Resolver
     	});
    
     	loadInitializers(Eng, modulePrefix);
    
     	export default Eng;
  • In /lib/[Engine Name]/addon/routes/application.js

     	import Ember from 'ember';
    
     	const { Route } = Ember;
    
     	import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
    
     	export default Route.extend(ApplicationRouteMixin, {
     		sessionAuthenticated() { } // Needed so sessionAuthenticated only runs on main login
     	});
  • In /lib/[Engine Name]/addon/routes/index.js

     	import Ember from 'ember';
    
     	const { Route } = Ember;
    
     	import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
    
     	export default Route.extend(AuthenticatedRouteMixin);
  • In /lib/[Engine Name]/addon/routes/login.js

     	import Ember from 'ember';
    
     	const { Route } = Ember;
    
     	export default Route.extend({
     		beforeModel() {
     			this._super(...arguments);
     			this.transitionToExternal('login'); // Transition to whatever you called your external login
     		}
     	});
import Ember from 'ember';
import Resolver from './resolver';
import config from './config/environment';
import loadInitializers from 'ember-load-initializers';
const { Application } = Ember;
const { modulePrefix, podModulePrefix } = config;
const dependencies = {
dependencies: {
services: ['session', 'cookies'], // You need both session and cookies to be able to route back into the engine from the external login
externalRoutes: {
'login': 'login' // you can call it whatever you want
}
}
};
const engines = { // add new engines here
<engine-name>: dependencies,
};
Ember.MODEL_FACTORY_INJECTIONS = true;
let App = Application.extend({
podModulePrefix,
modulePrefix,
Resolver,
engines
});
loadInitializers(App, modulePrefix);
export default App;
import Resolver from './resolver';
import Engine from 'ember-engines/engine';
import config from './config/environment';
import loadInitializers from 'ember-load-initializers';
const { modulePrefix } = config;
const dependencies = {
services: ['session', 'cookies'], // Need both to route into engine after authentication
externalRoutes: ['login']
};
const Eng = Engine.extend({
dependencies,
modulePrefix,
Resolver
});
loadInitializers(Eng, modulePrefix);
export default Eng;
import buildRoutes from 'ember-engines/routes';
export default buildRoutes(function() {
this.route('index', { path: '' });
this.route('login');
});
import Ember from 'ember';
const { Route } = Ember;
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Route.extend(ApplicationRouteMixin, {
sessionAuthenticated() { } // Needed so sessionAuthenticated only runs on main login
});
import Ember from 'ember';
const { Route } = Ember;
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Route.extend(AuthenticatedRouteMixin);
import Ember from 'ember';
const { Route } = Ember;
export default Route.extend({
beforeModel() {
this._super(...arguments);
this.transitionToExternal('login'); // Transition to whatever you called your external login
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment