Skip to content

Instantly share code, notes, and snippets.

@nexneo

nexneo/auth.js Secret

Last active June 25, 2020 12:06
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 nexneo/fb203e08818e8780ef7de421a36144f6 to your computer and use it in GitHub Desktop.
Save nexneo/fb203e08818e8780ef7de421a36144f6 to your computer and use it in GitHub Desktop.
import { context } from '@redwoodjs/api/dist/globalContext'
import { AuthenticationError } from '@redwoodjs/api'
import { db } from 'src/lib/db'
const cookie = require('cookie')
export const userContext = async ({ event, _context }) => {
const cookie = cookie.parse(event.headers.cookie || '')
console.log(cookie)
if (!cookie) {
return {}
}
const data //[redacted] read session data from cookie
const currentPerson = await db.person.findOne({
where: { id: Number(data.person_id) },
})
return { currentPerson }
}
export const requireAuth = () => {
console.log(context)
if (!context.currentPerson) {
throw new AuthenticationError("You don't have permission to do that.")
}
return context.currentUser
}
import { userContext } from 'src/lib/auth'
const schemas = importAll('api', 'graphql')
const services = importAll('api', 'services')
export const handler = createGraphQLHandler({
context: userContext,
schema: makeMergedSchema({
schemas,
services: makeServices({ services }),
}),
db,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment