GraphQL API authentication with JWT tokens in headless Magento
Asked 1/14/2024
890 views
Setting up a React Native app with Magento 2.4.5 backend. Need help implementing secure JWT authentication flow for GraphQL API calls.
## Current Setup
- Magento 2.4.5 Open Source
- React Native 0.72
- Apollo GraphQL Client
- Custom JWT module installed
## Issue
Current setup returns 401 unauthorized when trying to access customer data after login:
```graphql
mutation {
generateCustomerToken(email: "test@example.com", password: "password123") {
token
}
}
```
The token is generated successfully, but subsequent requests fail:
```graphql
query {
customer {
firstname
lastname
email
}
}
```
## Headers Sent
```
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...
Content-Type: application/json
```
Getting "The current customer isn't authorized" error. What am I missing in the authentication flow?