- Encode a JSON Web Token
- Verify and decode a JSON Web Token
- Request GraphQL-API using JSON Web Token
- Usage of JWT authentication for authorization
Generates a new JSON Web Token
subkit jwt \
--encode '{"username":"go@subkit.io"}' \
--secret SuperSecretsubkit jwt \
--decode eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
--secret SuperSecretsubkit request \
--token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
--url http://localhost:8080/graphql \
--query 'query loadAll {items {id email}}'If authentication is successful, the user information (JWT payload) is passed on to the resolver function context. This allows information about the current user to be used in the resolver functions.
export const resolvers = {
// ...
Query: {
items: (parent, args, context, info) => {
// access to current user for authorization or user specific data fetching
if (!context.user) return null;
if (context.user.username !== 'go@subkit.io') return null;
return context.loaders.items;
},
},
// ...
}