Commit 262bee7e authored by Bertrand PINEL's avatar Bertrand PINEL

Complete version with JHipster adapter and serializer

parent d16f7520
import DS from 'ember-data';
import ENV from './../config/environment';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.JSONAPIAdapter.extend({
namespace: 'api'
var JhipsterAdapter = DS.RESTAdapter.extend(DataAdapterMixin, {
namespace: 'api',
authorizer: 'authorizer:token',
handleResponse(status, headers, payload, requestData) {
var typeKey = requestData.url.slice(this.namespace.length+2, requestData.url.length);
var pos = typeKey.indexOf("/"); // for response on URL with an Id
if (pos >= 1) {
typeKey = typeKey.slice(0,pos-1);
}
var newPayload = {};
newPayload[typeKey] = payload;
// todo : how to handle embedded relationships...
return this._super(status, headers, newPayload, requestData);
},
});
var MirageAdapter = DS.JSONAPIAdapter.extend({
namespace: 'api',
shouldBackgroundReloadRecord() {
return false;
}
});
var FinalAdapter;
if (ENV.APP.jhipster && ENV.APP.proxy) {
FinalAdapter = JhipsterAdapter;
} else {
FinalAdapter = MirageAdapter;
}
export default FinalAdapter;
import Ember from 'ember';
import ENV from './../config/environment';
export default Ember.Controller.extend({
session: Ember.inject.service('session'),
isJhipster: ENV.APP.jhipster,
actions: {
invalidateSession() {
this.get('session').invalidate();
this.transitionToRoute('login');
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
session: Ember.inject.service(),
actions: {
authenticate: function() {
var credentials = this.getProperties('identification', 'password'),
authenticator = 'authenticator:jwt';
var ctx = this;
this.get('session').authenticate(authenticator, credentials).then(function () {
ctx.transitionToRoute('racks');
});
}
}
});
......@@ -9,6 +9,9 @@ const Router = Ember.Router.extend({
Router.map(function() {
this.route('racks');
this.route('rack', { path: '/rack/:rack_id' });
if (config.APP.jhipster) {
this.route('login');
}
});
export default Router;
import Ember from 'ember';
import ENV from './../config/environment';
export default Ember.Route.extend({
beforeModel() {
this.transitionTo('racks');
}
session: Ember.inject.service(),
beforeModel() {
if (ENV.APP.jhipster && ENV.APP.proxy && !this.get('session').get('isAuthenticated')) {
this.transitionTo('login');
} else {
this.transitionTo('racks');
}
},
});
import Ember from 'ember';
import ENV from './../config/environment';
export default Ember.Route.extend({
session: Ember.inject.service(),
model() {
// Preload all bottles
let bottles = this.store.findAll('bottle', { reload: true });
return this.store.findAll('rack', {include: 'bottles'});
}
// Preload all bottles
let bottles = this.store.findAll('bottle', { reload: true });
return this.store.findAll('rack', {include: 'bottles'});
}
});
import DS from 'ember-data';
import ENV from './../config/environment';
var JhipsterSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin,{
attrs: {
rack: { embedded: 'always'}
}
}
);
var MirageSerializer = DS.JSONAPISerializer.extend({
});
var FinalSerializer;
if (ENV.APP.jhipster && ENV.APP.proxy) {
FinalSerializer = JhipsterSerializer;
} else {
FinalSerializer = MirageSerializer;
}
export default FinalSerializer;
......@@ -3,7 +3,15 @@
<div class="page-header">
<h1>Ember Cellar</h1>
<p class="lead">Basic layout using Bootstrap for a simple wine application</p>
</div>
{{outlet}}
{{#if isJhipster}}
<p class="text-right">
{{#if session.isAuthenticated}}
<a {{action 'invalidateSession'}}>Logout</a>
{{else}}
{{#link-to 'login'}}Login{{/link-to}}
{{/if}}
</p>
{{/if}}
</div>
{{outlet}}
</div>
{{!-- app/templates/login.hbs --}}
<form {{action 'authenticate' on='submit'}}>
<label for="identification">Login</label>
{{input id='identification' placeholder='Enter Login' value=identification}}
<label for="password">Password</label>
{{input id='password' placeholder='Enter Password' type='password' value=password}}
<button type="submit">Login</button>
{{#if errorMessage}}
<p>{{errorMessage}}</p>
{{/if}}
</form>
/* eslint-env node */
'use strict';
var fs = require('fs');
module.export /* jshint node: true */
function usingProxy() {
var usingProxyArg = !!process.argv.filter(function (arg) {
return arg.indexOf('--proxy') === 0 || arg.indexOf('-pr') === 0 || arg.indexOf('-pxy') === 0;
}).length;
var hasGeneratedProxies = false;
var proxiesDir = process.env.PWD + '/server/proxies';
try {
fs.lstatSync(proxiesDir);
hasGeneratedProxies = true;
} catch (e) {}
return usingProxyArg || hasGeneratedProxies;
}
function usingJhipster() {
let isJhipster = !!process.argv.filter(function (arg) {
return arg.indexOf('--jhipster') === 0 || arg.indexOf('-jhi') === 0
}).length;
return isJhipster;
}
module.exports = function(environment) {
var ENV = {
......@@ -20,6 +45,8 @@ module.exports = function(environment) {
APP: {
// Here you can pass flags/options to your application instance
// when it is created
proxy: usingProxy(), // todo set proxy according to the --proxy option passed to ember cli
jhipster: usingJhipster()
}
};
......@@ -45,6 +72,16 @@ module.exports = function(environment) {
if (environment === 'production') {
}
ENV['ember-simple-auth'] = {
authorizer: 'authorizer:token'
};
ENV['ember-simple-auth-token'] = {
refreshAccessTokens: true,
refreshLeeway: 300, // Refresh the token 5 minutes (300s) before it expires.
serverTokenEndpoint: '/api/authenticate',
identificationField: 'username',
passwordField: 'password',
tokenPropertyName: 'id_token'
};
return ENV;
};
import { moduleFor, test } from 'ember-qunit';
moduleFor('controller:application', 'Unit | Controller | application', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});
// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.subject();
assert.ok(controller);
});
import { moduleFor, test } from 'ember-qunit';
moduleFor('controller:login', 'Unit | Controller | login', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});
// Replace this with your real tests.
test('it exists', function(assert) {
let controller = this.subject();
assert.ok(controller);
});
import Ember from 'ember';
import { initialize } from 'ember-cellar/initializers/jwt-token';
import { module, test } from 'qunit';
import destroyApp from '../../helpers/destroy-app';
module('Unit | Initializer | jwt token', {
beforeEach() {
Ember.run(() => {
this.application = Ember.Application.create();
this.application.deferReadiness();
});
},
afterEach() {
destroyApp(this.application);
}
});
// Replace this with your real tests.
test('it works', function(assert) {
initialize(this.application);
// you would normally confirm the results of the initializer here
assert.ok(true);
});
import Ember from 'ember';
import { initialize } from 'ember-cellar/instance-initializers/jwt-token';
import { module, test } from 'qunit';
import destroyApp from '../../helpers/destroy-app';
module('Unit | Instance Initializer | jwt token', {
beforeEach() {
Ember.run(() => {
this.application = Ember.Application.create();
this.appInstance = this.application.buildInstance();
});
},
afterEach() {
Ember.run(this.appInstance, 'destroy');
destroyApp(this.application);
}
});
// Replace this with your real tests.
test('it works', function(assert) {
initialize(this.appInstance);
// you would normally confirm the results of the initializer here
assert.ok(true);
});
import { moduleFor, test } from 'ember-qunit';
moduleFor('route:login', 'Unit | Route | login', {
// Specify the other units that are required for this test.
// needs: ['controller:foo']
});
test('it exists', function(assert) {
let route = this.subject();
assert.ok(route);
});
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('application', 'Unit | Serializer | application', {
// Specify the other units that are required for this test.
needs: ['serializer:application']
});
// Replace this with your real tests.
test('it serializes records', function(assert) {
let record = this.subject();
let serializedRecord = record.serialize();
assert.ok(serializedRecord);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment