@ampatspell/ember-cli-relax-session
v0.0.3
Published
Relax session ember-cli addon
Downloads
3
Maintainers
Readme
ember-cli-relax-session
Relax session ember-cli addon
Install
$ ember install @ampatspell/ember-cli-relax-session
Configure Session
Optionally extend Session service:
// app/services/session.js
import Ember from 'ember';
import Session from 'relax-session/services/session';
export default Session.extend({
onRestored: function() {
},
onSaved: function() {
},
});
Extend Session model:
// app/models/session.js
import Session from "relax-session/models/session";
export default Session.extend({
authenticated: DS.attr('boolean'),
roles: DS.attr('raw'),
author: DS.belongsTo('author', {async: true}),
// load additional relationships after login if necessary
loadRelationships: function() {
return this.get('author.promise');
},
});
Restore session in application route:
// app/routes/application.js
import Ember from 'ember';
export default Route.extend({
session: Ember.inject.service(),
beforeModel: function() {
this._super(...arguments);
return this.get('session').restoreOnce();
}
});
Configure Requirements
Extend Requirements service:
// app/services/requirements.js
import Requirements from 'relax-session/services/requirements';
import requirements from '../requirements';
export default Requirements.extend({
requirements: requirements,
});
Add requirements
import Ember from 'ember';
export default {
'anonymous': function() {
if(!this.get('authenticated')) {
return;
}
return 'logged-in';
},
'logged-in': function() {
if(this.get('authenticated')) {
return;
}
return 'login';
},
'moderator-or-author': function(model) {
Ember.assert("model is required", Ember.typeOf(model) === 'instance');
if(!this.get('authenticated')) {
return 'login';
}
if(this.get('author.content') === model) {
return "is-author";
}
if(this.get('roles').indexOf('moderator') !== -1) {
return "is-moderator";
}
return 'denied';
}
};
Add RequirementRouteMixin:
// app/routes/base.js
import Ember from 'ember';
import RequirementRouteMixin from 'relax-session/mixins/requirement-route';
export default Ember.Route.extend(RequirementRouteMixin, {
// before model requirement
require: 'logged-in',
// requirement response to route name mapping
requireMapping: {
'login': 'sessions.new',
'denied': 'sessions.denied',
'logged-in': 'index',
'is-author': null,
'is-moderator': null,
},
});
Optionally add after model requirements:
// app/some-route.js
import Route from './base';
export default Route.extend({
model: function(params) {
return this.store.find('thing', params.id);
},
afterModel: function(thing) {
var author = thing.get('author');
return this.validateRequirement('moderator-or-author', author);
},
});