As follows is a brief overview of how the plugin works.
- Visit your WordPress site's admin Dashboard > GraphQL > Settings > Headless Login, and enable/configure the authentication providers you want to use and any other plugin settings.
- In your frontend app, pass the required data to the
login
(orlinkUserIdentity
) mutation to authenticate a user.
- For Email/Password: Pass the user's email and password to the
LoginInput.credentials
input arg. - For OAuth Providers:
- Get the Client
authorizationUrl
from theRootQuery.loginClients
field in GraphQL to use in your client app (or DIY with a frontend dependency like Auth.js). - When your frontend user clicks and visits the
authorizationUrl
, they'll be propmpted to login with the provider. If authorization is successful, they (along with the Authorization response ) will be redirected to theredirectUri
you configured in the Provider's settings. - The Provider's authorization response should then be passed via the
LoginInput.oauthResponse
input arg.
- Get the Client
- For Site Token:
- Set the special token you set on the request header you configured in the Provider settings.
- Pass the
user_identity
of the user you want to authenticate to theLoginInput.identity
input arg. This could be a username, email, or resource owner ID from a frontend dependency like Auth.js. Note: Whenever possible, the mutations should be called from a server-side environment (like Next.js API route or AWS Lambda) to avoid exposing authentication and authroization credentials to the public.
- Your WordPress server will then:
- Validate the authentication response.
- Fetch the Resource Owner's profile data from the provider.
- (optional) Create a new WordPress user and/or link an existing WordPress user to the Provider's Resource Owner.
- Generate and returns a JWT
authToken
andrefreshToken
for the user to use in future requests.
- Your frontend app should and and store the
authToken
andrefreshToken
in a secure location (like a Web Worker or secure cookie) for future requests.
- Authentication tokens should be passed in the Authorization Headers of future GraphQL requests.
- Refresh tokens can be exchanged for a new auth token without requiring the user to reauthenticate.
- Woocommerce - If you're using WPGraphQL for WooCommerce, you should store and pass the
wooSessionToken
to thewoocommerce-session
header.
- You can get a new
authToken
andrefreshToken
for the user in the response of an authenticated query by querying for theRootQuery.viewer.auth.authToken
, or exchanged a storedrefreshToken
for a newauthToken
using therefreshToken
mutation. - You can log the user out of all other devices by using
refreshUserSecret
orrevokeUserSecret
mutations.