Created
May 22, 2018 10:27
-
-
Save lovegrovegeorge/2c53c1928ae996658526bfa7ad643897 to your computer and use it in GitHub Desktop.
Utility functions used in a Prisma server
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
const jwt = require('jsonwebtoken') | |
function getUserId(ctx) { | |
let authorization = ctx.request ? ctx.request.get('authorization') : null | |
// Get from subscription connection context | |
if (!authorization) { | |
authorization = ctx.connection ? ctx.connection.context.authorization : null | |
} | |
if (authorization) { | |
const token = authorization.replace('Bearer ', '') | |
const { userId } = jwt.verify(token, process.env.APP_SECRET) | |
return userId | |
} | |
throw new AuthError() | |
} | |
class AuthError extends Error { | |
constructor() { | |
super('Not authorized') | |
} | |
} | |
module.exports = { | |
getUserId, | |
AuthError | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment