WPGraphQL JWT Authentication
This FREE plugin enables you to authenticate to WordPress using a GraphQL login mutation and get JWT tokens in response and use to make authenticated requests to WordPress.
Official PluginView Plugin on Github
This plugin extends the WPGraphQL plugin to provide authentication using JWT (JSON Web Tokens)
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
This plugin was initially based off the
wp-api-jwt-auth plugin by Enrique Chavez (https://github.com/Tmeister), but modified (almost completely) for use with the WPGraphQL plugin.
You can install and activate the plugin like any WordPress plugin. Download the .zip from Github and add to your plugins directory, then activate.
JWT uses a Secret defined on the server to validate the signing of tokens.
It's recommended that you use something like the WordPress Salt generator (https://api.wordpress.org/secret-key/1.1/salt/) to generate a Secret.
You can define a Secret like so:
Or you can use the filter
graphql_jwt_auth_secret_key to set a Secret like so:
This secret is used in the encoding and decoding of the JWT token. If the Secret were ever changed on the server, ALL tokens that were generated with the previous Secret would become invalid. So, if you wanted to invalidate all user tokens, you can change the Secret on the server and all previously issued tokens would become invalid and require users to re-authenticate.
- Learn more about JWT: https://jwt.io/introduction/
In order to use this plugin, your WordPress environment must support the HTTP_AUTHORIZATION header. In some cases, this header is not passed to WordPress because of some server configurations.
Depending on your particular environment, you may have to research how to enable these headers, but in Apache, you can do the following in your
For NGINX, this may work: https://serverfault.com/questions/511206/nginx-forward-http-auth-user#answer-511612
This plugin adds a new
login mutation to the WPGraphQL Schema.
This can be used like so:
authToken that is received in response to the login mutation can then be stored in local storage (or similar) and
used in subsequent requests as an HTTP Authorization header to Authenticate the user prior to execution of the
- Set authorization header in Apollo Client: https://www.apollographql.com/docs/react/networking/authentication/#header
- Set authorization header in Relay Modern: https://relay.dev/docs/en/network-layer.html
- Set authorization header in Axios: https://github.com/axios/axios#axioscreateconfig
The plugin offers some filters to hook into.
Note: For security, we highly recommend, that the Auth Token is short lived. So do not set this higher than 300 seconds unless you know what you are doing.
- Argument: Expiration in seconds
- Default: 300