Created
February 21, 2019 01:50
-
-
Save Wellers0n/3b35154df0e2117d807e750da029166b to your computer and use it in GitHub Desktop.
authentication token with KoaJS
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 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