Last active
August 29, 2015 13:57
-
-
Save robwormald/9441746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
authenticate : function(req,res){ | |
var username = req.param('username') | |
var password = req.param('password') | |
if(!username || !password){ | |
return res.json(403,{err : 'username and password required'}) | |
} | |
User.findOneByUsername(username,function(err,user){ | |
if(!user){ | |
return res.json(403,{err : 'invalid username or password'}) | |
} | |
User.validPassword(password,user,function(err,valid){ | |
if(err){ | |
return res.json(403,{err : 'forbidden'}) | |
} | |
if(!valid){ | |
return res.json(403,{err : 'invalid username or password'}) | |
} | |
else{ | |
//TODO grab auth permission stuff to encode into token. | |
res.json({user : user, token : sailsTokenAuth.issueToken(user.id)}) | |
} | |
}) | |
}) | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
authorization: function authorizeAttemptedSocketConnection(reqObj, cb) { | |
// Any data saved in `handshake` is available in subsequent requests | |
// from this as `req.socket.handshake.*` | |
if(reqObj.query.token){ | |
sailsTokenAuth.verifyToken(reqObj.query.token,function(err,tokenData){ | |
if(tokenData){ | |
reqObj.handshake = tokenData; | |
cb(null,true) | |
} | |
else{ | |
cb(null,false) | |
} | |
}) | |
} | |
else{ | |
reqObj.handshake = {authenticated : false} | |
cb(null,true) | |
} | |
// | |
// to allow the connection, call `cb(null, true)` | |
// to prevent the connection, call `cb(null, false)` | |
// to report an error, call `cb(err)` | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jwt = require('jsonwebtoken') | |
var socketjwt = require('socketio-jwt') | |
module.exports.issueToken = function(payload){ | |
var token = jwt.sign(payload,process.env.TOKEN_SECRET) | |
return token; | |
} | |
module.exports.verifyToken = function(token,verified){ | |
return jwt.verify(token,process.env.TOKEN_SECRET,{},verified) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment