Authentication: for this request, who is the user
While Bunjil ships with a powerful PBAC authorization engine, it does not come with any authentication logic. Authentication can be a tricky thing to get right, and is typically different depending on the project or organization.
Bunjil provides a single middleware for you to process authentication per request. Itβs expected that you would also add a login mutation to your schema.
Authentication middleware
The authentication middleware is a koa middleware, that runs for every request just before the GraphQL query is processed.
Your middleware should take the ctx.request object, determine the currently logged in user (if any) and return information about the user onto the ctx.user object.
Bunjil expects a user object of the following shape:
type ctx.user = { |
You can add anything else you may need for authorization to ctx.user, as the ctx.user and ctx.request are both passed into the GraphQL context object.
Example
This is an example of a simple authentication callback that uses a server signed JWT. The important bit is extracting the id and an array of roles that we put on the ctx.user object.
import * as jwt from "jsonwebtoken"; |