Skip to content

Instantly share code, notes, and snippets.

@lovegrovegeorge
Created May 22, 2018 10:27
Show Gist options
  • Save lovegrovegeorge/2c53c1928ae996658526bfa7ad643897 to your computer and use it in GitHub Desktop.
Save lovegrovegeorge/2c53c1928ae996658526bfa7ad643897 to your computer and use it in GitHub Desktop.
Utility functions used in a Prisma server
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