#twitter oauth with convex and better-auth
1 messages · Page 1 of 1 (latest)
The error you're seeing:
[Better Auth]: ' {
error: 'unauthorized_client',
error_description: 'Missing valid authorization header',
status: 401,
statusText: 'Unauthorized'
}
indicates that the Twitter OAuth flow with Better Auth and Convex is failing due to a missing or invalid authorization header. This typically means that the request to the Better Auth server is not including the required authentication credentials (such as a session token or cookie).
Based on the knowledge sources, here are some things to check:
-
Headers Must Be Passed for Server-Side Calls:
When making server-side calls to Better Auth (such as usingauth.apimethods), you must pass the correct headers so that session cookies can be parsed and validated. The recommended approach is to use thegetHeadersmethod from the Better Auth component and pass those headers to your API call. For example:const headers = await betterAuthComponent.getHeaders(ctx); const token = await auth.api.getAccessToken({ body: { providerId: args.providerId }, headers, });If you don't include the headers, Better Auth will not be able to authenticate the request and will return a 401 error like the one you're seeing.
Basic Usage: Using server methods withauth.api -
Check the Client-Side Authentication State:
If you're using Better Auth's session hooks (likeuseSession()), be aware that Better Auth may reflect an authenticated user before Convex does. For Convex queries that require authentication, you should use Convex's<Authenticated>component or theuseConvexAuth()hook to ensure the user is authenticated from Convex's perspective.
Basic Usage: Authorization
-
Inspect Network Traffic:
Use your browser's network tools to ensure that the authentication token is being sent with requests to the backend. For websocket-based clients, check that theAuthenticatemessage includes a valid token. For HTTP-based clients, ensure theAuthorizationheader is present and correct.
Debugging Authentication -
Better Auth and Convex Integration:
Make sure you are using the recommended versions of the packages and that your configuration matches the official guides. If you are using the Better Auth component, ensure it is registered and configured as shown in the installation guide.
TanStack Start Installation Guide
If after checking these points you are still seeing the error, double-check that your Twitter provider configuration (client ID/secret) is correct and that your environment variables are set up properly.
If you need to debug further, try logging the headers you are sending to Better Auth and ensure they include the necessary session information.