Contents
Installation
npm install nexus-plugin-jwt-auth
Example Usage
Find full examples using both the built in permissions system or by leveragering nexus-plugin-shield:
- Basic Permissions - examples/basic-permissions
- Shield - examples/shield
Setup
// app.ts // Enables the JWT Auth plugin without permissionsuseauth
You may now access the token
object and it's properties on the Nexus context
.
Permissions
Basic permissions can be added too.
// app.ts // Define the paths you'd like to protect // Enables the JWT Auth plugin with permissionsuseauth
Stored Properties
You can also access properties stored in the token.
In this example I sign the token on signup or login then store the userId in the token to be accessed directly in a query or mutation to find the authed user.
// Query.ts schema.queryType
Use cookie instead of Authorization header
// Add the cookie-parser middleware to Expressserver.express.usecookieParser // Enables the JWT Auth plugin with cookiesuseauth
Don't forget to set credentials: true
in your GraphQL client or the cookie will not be sent to the server.
Contributing
Please read CONTRIBUTING.md