Skip to content

Instantly share code, notes, and snippets.

@Wellers0n
Created February 21, 2019 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wellers0n/3b35154df0e2117d807e750da029166b to your computer and use it in GitHub Desktop.
Save Wellers0n/3b35154df0e2117d807e750da029166b to your computer and use it in GitHub Desktop.
authentication token with KoaJS
import jwt from 'jsonwebtoken';
const authMiddleware = async (ctx, next) => {
const { authorization } = ctx.request.headers;
if (authorization) {
jwt.verify(authorization, 'batman', (err, decoded) => {
if (err) return ctx.body = 401
ctx.state.user = decoded.id;
console.log(ctx.state.user)
})
}else{
return ctx.status = 403
}
await next();
}
export default authMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment