javascript - Custom sessions in Ember Js. How to set/access session information -
so followed youtube tutorial on how authorize using session tokens. main session code following.
//app/services/session.js import ember 'ember'; export default ember.service.extend({ token: null, authenticate(log, pass) { return ember.$.ajax({ method: 'post', url: '/token', data: {username: log, password: pass} }).then((info)=>{ this.set('token',info.access_token); }); } });
server set on here.
//server/index.js const bodyparser = require('body-parser'); module.exports = function(app) { app.use(bodyparser.urlencoded({ extended: true})); app.post('/token', function(req, res){ console.log(res); if(req.body.username === 'erik' && req.body.password === 'password') { res.send( { access_token: 'secretcode'}); } else { res.status(400).send({ error: 'invalid_grant'}); } }); app.get('/api/students', function(req, res) { if( req.headers.authorization !== 'bearer secretcode'){ return res.status(401).send('unauthorized'); } return res.status(200).send({ students: [ { id: 1, name: 'erik', age: 23}, { id: 2, name: 'bob', age: 52} ] }); }); };
so how set multiple info on session token. user_id
? give access page if session contains user_id
or using in controller
click_if_authenticated(){ //use session.user_id here}
i suggest authorize users ember-simple-auth library. gives possiblity check status of session if isauthenthicaded , display content need. possible define routes require authenticated user , ones authentication not required etc. think easier approach 1 trying make.
Comments
Post a Comment