Basic user/session integration
This commit is contained in:
		
							parent
							
								
									5048fb7695
								
							
						
					
					
						commit
						2b6d7fecbb
					
				
					 4 changed files with 108 additions and 22 deletions
				
			
		
							
								
								
									
										22
									
								
								app/initializers/custom-session.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/initializers/custom-session.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -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") | ||||
|     }); | ||||
|   } | ||||
| }; | ||||
|  | @ -2,25 +2,29 @@ | |||
|     {{#link-to 'index' class='logo'}} | ||||
|       <img src="img/logo.png" width="120"> | ||||
|     {{/link-to}} | ||||
|     <ul> | ||||
|       {{#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}} | ||||
|     </ul> | ||||
| 
 | ||||
|     <p class="foot"> | ||||
|       {{#if session.isAuthenticated}} | ||||
|     {{#if session.isAuthenticated}} | ||||
|       <ul> | ||||
|         {{#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}} | ||||
|       </ul> | ||||
|       <p class="foot"> | ||||
|         {{session.currentUser.name}}<br> | ||||
|         <a {{ action 'invalidateSession' }}>Logout</a> | ||||
|       {{else}} | ||||
|         {{#link-to 'login'}}Login{{/link-to}} | ||||
|       {{/if}} | ||||
|     </p> | ||||
|       </p> | ||||
|     {{else}} | ||||
|       <p class="foot"> | ||||
|         {{#link-to 'login' Login}}Login{{/link-to}} | ||||
|         <br> | ||||
|         Sign Up | ||||
|       </p> | ||||
|     {{/if}} | ||||
|   </div> | ||||
| 
 | ||||
|   <div class="flakes-content"> | ||||
|  |  | |||
|  | @ -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'}); | ||||
|  |  | |||
							
								
								
									
										23
									
								
								tests/unit/initializers/custom-session-test.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								tests/unit/initializers/custom-session-test.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -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); | ||||
| }); | ||||
		Reference in a new issue
	
	 Matthew Dillon
						Matthew Dillon