-
Notifications
You must be signed in to change notification settings - Fork 47
Description
Certainly! Here's an updated guide that includes the JWT Authentication plugin:
-
Install and activate the WPGraphQL plugin and the WPGraphQL-JWT-Authentication plugin.
-
Define a secret key for JWT Authentication. This key will be used to encode and decode JSON Web Tokens. You can define the key in your
wp-config.phpfile like this:define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', 'your-secret-key' );Replace
your-secret-keywith a unique, randomly generated string of characters. You can use a tool like the WordPress Salt generator (https://api.wordpress.org/secret-key/1.1/salt/) to generate a random string.Alternatively, you can use the
graphql_jwt_auth_secret_keyfilter to define the key in a custom function:add_filter( 'graphql_jwt_auth_secret_key', function() { return 'your-secret-key'; } ); -
Ensure that your server is passing the
HTTP_AUTHORIZATIONheader to WordPress. In some cases, this header may not be passed due to server configurations. You can enable the header in your server configuration file, such as.htaccessfile for Apache, or by following instructions specific to your server. -
Use the
loginmutation to authenticate a user and retrieve a JWT token. Theloginmutation takes aLoginUserInputinput type and returns anauthTokenanduserobject withidandnamefields. You can store theauthTokenin local storage (or similar) and use it in subsequent requests as an HTTP Authorization header to authenticate the user.Here's an example query for the
loginmutation:mutation LoginUser { login( input: { clientMutationId: "uniqueId", username: "your_login", password: "your password" } ) { authToken user { id name } } }Replace
your_loginandyour_passwordwith the user's login credentials. You can use a tool like GraphiQL or Insomnia to send the query to your WordPress site and retrieve the token. -
Use the
authTokenretrieved from theloginmutation as an HTTP Authorization header in subsequent requests to authenticate the user. The header should be in the formatAuthorization: Bearer {authToken}.The method for setting the Authorization header varies depending on your HTTP client library. For example, you can set the header in Apollo Client like this:
const client = new ApolloClient({ uri: 'http://your-wordpress-site/graphql', headers: { Authorization: `Bearer ${authToken}`, }, }); -
Use the
registerUsermutation to create a new user with a JWT token. TheregisterUsermutation takes aRegisterUserInputinput type and returns auserobject withjwtAuthTokenandjwtRefreshTokenfields.Here's an example query for the
registerUsermutation:mutation RegisterUser { registerUser( input: { clientMutationId: "uniqueId", username: "your_username", password: "your_password", email: "your_email" } ) { user { jwtAuthToken jwtRefreshToken } } }Replace
your_username,your_password, andyour_emailwith the desired values for the new user. -
Use the
refreshJwtAuthTokenmutation to refresh an expired JWT token. The `refreshJwtAuthToken