Created
December 16, 2016 10:59
-
-
Save bertho-zero/5cdeabb77be98bc159b1e5d75c6af429 to your computer and use it in GitHub Desktop.
This file contains 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
import { verifyJWT } from 'feathers-authentication/lib/utils'; | |
export default function socketAuth(app) { | |
return (socket, next) => { | |
const { cookie } = socket.request.headers; | |
const cookies = cookie && cookie.split('; ') | |
.reduce((prev, nextCookie) => { | |
const [name, value] = nextCookie.split('='); | |
return { | |
...prev, | |
[name]: value | |
}; | |
}, {}); | |
const accessToken = cookies['feathers-jwt'] || null; | |
socket._feathers = {}; | |
if (!accessToken) return next(); | |
verifyJWT(accessToken, app.get('auth')) | |
.then(payload => app.service('users').get(payload.userId)) | |
.then(user => { | |
Object.assign(socket.feathers, { | |
accessToken, | |
user, | |
authenticated: true | |
}); | |
next(); | |
}) | |
.catch(() => next()); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment