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'}} |     {{#link-to 'index' class='logo'}} | ||||||
|       <img src="img/logo.png" width="120"> |       <img src="img/logo.png" width="120"> | ||||||
|     {{/link-to}} |     {{/link-to}} | ||||||
|     <ul> |     {{#if session.isAuthenticated}} | ||||||
|       {{#link-to 'strains' tagName='li' href=false}} |       <ul> | ||||||
|         {{#link-to 'strains'}}Strains{{/link-to}} |         {{#link-to 'strains' tagName='li' href=false}} | ||||||
|       {{/link-to}} |           {{#link-to 'strains'}}Strains{{/link-to}} | ||||||
|       {{#link-to 'characteristics' tagName='li' href=false}} |         {{/link-to}} | ||||||
|         {{#link-to 'characteristics'}}Characteristics{{/link-to}} |         {{#link-to 'characteristics' tagName='li' href=false}} | ||||||
|       {{/link-to}} |           {{#link-to 'characteristics'}}Characteristics{{/link-to}} | ||||||
|       {{#link-to 'about' tagName='li' href=false}} |         {{/link-to}} | ||||||
|         {{#link-to 'about'}}About{{/link-to}} |         {{#link-to 'about' tagName='li' href=false}} | ||||||
|       {{/link-to}} |           {{#link-to 'about'}}About{{/link-to}} | ||||||
|     </ul> |         {{/link-to}} | ||||||
| 
 |       </ul> | ||||||
|     <p class="foot"> |       <p class="foot"> | ||||||
|       {{#if session.isAuthenticated}} |         {{session.currentUser.name}}<br> | ||||||
|         <a {{ action 'invalidateSession' }}>Logout</a> |         <a {{ action 'invalidateSession' }}>Logout</a> | ||||||
|       {{else}} |       </p> | ||||||
|         {{#link-to 'login'}}Login{{/link-to}} |     {{else}} | ||||||
|       {{/if}} |       <p class="foot"> | ||||||
|     </p> |         {{#link-to 'login' Login}}Login{{/link-to}} | ||||||
|  |         <br> | ||||||
|  |         Sign Up | ||||||
|  |       </p> | ||||||
|  |     {{/if}} | ||||||
|   </div> |   </div> | ||||||
| 
 | 
 | ||||||
|   <div class="flakes-content"> |   <div class="flakes-content"> | ||||||
|  |  | ||||||
|  | @ -3,15 +3,51 @@ module.exports = function(app) { | ||||||
|   var jwt = require('jsonwebtoken'); |   var jwt = require('jsonwebtoken'); | ||||||
|   var authenticateRouter = express.Router(); |   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) { |   authenticateRouter.post('/', function(req, res) { | ||||||
|     // wait for a bit to simulate cold boot of heroku api
 |     // wait for a bit to simulate cold boot of heroku api
 | ||||||
|     var ms = 3000 + new Date().getTime(); |     var ms = 3000 + new Date().getTime(); | ||||||
|     while (new Date() < ms){} |     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({ |       var token = jwt.sign({ | ||||||
|         'name': 'Test User', |         'name': user.name, | ||||||
|         'role': 'A' |         'role': user.role | ||||||
|       }, 'secret', |       }, 'secret', | ||||||
|       { |       { | ||||||
|         expiresInMinutes: 60, |         expiresInMinutes: 60, | ||||||
|  | @ -19,7 +55,8 @@ module.exports = function(app) { | ||||||
|         subject: 'test', |         subject: 'test', | ||||||
|       }); |       }); | ||||||
|       res.send({ |       res.send({ | ||||||
|         'token': token |         'token': token, | ||||||
|  |         'user_id': user.id, | ||||||
|       }); |       }); | ||||||
|     } else { |     } else { | ||||||
|       res.status(401).send({'error':'Invalid username or password'}); |       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