From 2b6d7fecbb592c2d2011fa41d9a9f8d7db7e5771 Mon Sep 17 00:00:00 2001
From: Matthew Dillon
Date: Fri, 10 Apr 2015 13:32:44 -0800
Subject: [PATCH] Basic user/session integration
---
app/initializers/custom-session.js | 22 +++++++++
app/templates/application.hbs | 40 +++++++++--------
server/mocks/authenticate.js | 45 +++++++++++++++++--
.../unit/initializers/custom-session-test.js | 23 ++++++++++
4 files changed, 108 insertions(+), 22 deletions(-)
create mode 100644 app/initializers/custom-session.js
create mode 100644 tests/unit/initializers/custom-session-test.js
diff --git a/app/initializers/custom-session.js b/app/initializers/custom-session.js
new file mode 100644
index 0000000..8979e72
--- /dev/null
+++ b/app/initializers/custom-session.js
@@ -0,0 +1,22 @@
+// from: http://blog.willrax.com/fetching-the-current-user-with-simple-auth/
+import Ember from "ember";
+import Session from "simple-auth/session";
+
+export default {
+ name: "custom-session",
+ before: "simple-auth",
+ initialize: function(container) {
+ Session.reopen({
+ setCurrentUser: function() {
+ var id = this.get("user_id");
+ var self = this;
+
+ if (!Ember.isEmpty(id)) {
+ return container.lookup("store:main").find("user", id).then(function(user) {
+ self.set("currentUser", user);
+ });
+ }
+ }.observes("user_id")
+ });
+ }
+};
diff --git a/app/templates/application.hbs b/app/templates/application.hbs
index 5c84d01..f70063e 100644
--- a/app/templates/application.hbs
+++ b/app/templates/application.hbs
@@ -2,25 +2,29 @@
{{#link-to 'index' class='logo'}}
{{/link-to}}
-
- {{#link-to 'strains' tagName='li' href=false}}
- {{#link-to 'strains'}}Strains{{/link-to}}
- {{/link-to}}
- {{#link-to 'characteristics' tagName='li' href=false}}
- {{#link-to 'characteristics'}}Characteristics{{/link-to}}
- {{/link-to}}
- {{#link-to 'about' tagName='li' href=false}}
- {{#link-to 'about'}}About{{/link-to}}
- {{/link-to}}
-
-
-
+ {{#link-to 'strains' tagName='li' href=false}}
+ {{#link-to 'strains'}}Strains{{/link-to}}
+ {{/link-to}}
+ {{#link-to 'characteristics' tagName='li' href=false}}
+ {{#link-to 'characteristics'}}Characteristics{{/link-to}}
+ {{/link-to}}
+ {{#link-to 'about' tagName='li' href=false}}
+ {{#link-to 'about'}}About{{/link-to}}
+ {{/link-to}}
+
+
+
+ {{else}}
+
+ {{/if}}
diff --git a/server/mocks/authenticate.js b/server/mocks/authenticate.js
index 544bf59..7ae9434 100644
--- a/server/mocks/authenticate.js
+++ b/server/mocks/authenticate.js
@@ -3,15 +3,51 @@ module.exports = function(app) {
var jwt = require('jsonwebtoken');
var authenticateRouter = express.Router();
+ var USERS = [
+ {
+ id: 1,
+ email: 'testA',
+ name: 'Test Admin User',
+ role: 'A',
+ createdAt: "0001-01-01T00:00:00Z",
+ updatedAt: "0001-01-01T00:00:00Z",
+ deletedAt: null
+ },
+ {
+ id: 2,
+ email: 'testR',
+ name: 'Test Read User',
+ role: 'R',
+ createdAt: "0001-01-01T00:00:00Z",
+ updatedAt: "0001-01-01T00:00:00Z",
+ deletedAt: null
+ },
+ {
+ id: 3,
+ email: 'testW',
+ name: 'Test Write User',
+ role: 'W',
+ createdAt: "0001-01-01T00:00:00Z",
+ updatedAt: "0001-01-01T00:00:00Z",
+ deletedAt: null
+ }
+ ]
+
authenticateRouter.post('/', function(req, res) {
// wait for a bit to simulate cold boot of heroku api
var ms = 3000 + new Date().getTime();
while (new Date() < ms){}
- if (req.body.email === 'test' && req.body.password === 'test') {
+ if ((req.body.email === 'testA' || req.body.email === 'testR' || req.body.email === 'testW' )
+ && req.body.password === 'test') {
+ var user = USERS.filter(function(u) {
+ if (u.email == req.body.email) {
+ return u;
+ }
+ })[0];
var token = jwt.sign({
- 'name': 'Test User',
- 'role': 'A'
+ 'name': user.name,
+ 'role': user.role
}, 'secret',
{
expiresInMinutes: 60,
@@ -19,7 +55,8 @@ module.exports = function(app) {
subject: 'test',
});
res.send({
- 'token': token
+ 'token': token,
+ 'user_id': user.id,
});
} else {
res.status(401).send({'error':'Invalid username or password'});
diff --git a/tests/unit/initializers/custom-session-test.js b/tests/unit/initializers/custom-session-test.js
new file mode 100644
index 0000000..a447736
--- /dev/null
+++ b/tests/unit/initializers/custom-session-test.js
@@ -0,0 +1,23 @@
+import Ember from 'ember';
+import { initialize } from '../../../initializers/custom-session';
+import { module, test } from 'qunit';
+
+var container, application;
+
+module('CustomSessionInitializer', {
+ beforeEach: function() {
+ Ember.run(function() {
+ application = Ember.Application.create();
+ container = application.__container__;
+ application.deferReadiness();
+ });
+ }
+});
+
+// Replace this with your real tests.
+test('it works', function(assert) {
+ initialize(container, application);
+
+ // you would normally confirm the results of the initializer here
+ assert.ok(true);
+});