Skip to content

Instantly share code, notes, and snippets.

@bertho-zero
Created December 16, 2016 10:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bertho-zero/5cdeabb77be98bc159b1e5d75c6af429 to your computer and use it in GitHub Desktop.
Save bertho-zero/5cdeabb77be98bc159b1e5d75c6af429 to your computer and use it in GitHub Desktop.
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