loluL
Convex Community3mo ago
4 replies
lolu

getAuthUserId() Returns Null Despite Successful OAuth Sign-In

getAuthUserId() Returns Null Despite Successful OAuth Sign-In


Issue: getAuthUserId(ctx) returns
null
in all backend queries/mutations, even though OAuth sign-in succeeds and creates a valid session.

Environment:
- @convex-dev/auth: ^0.0.90
- Auth provider: Google OAuth

What Works:
- ✅ OAuth sign-in completes
- ✅ Session created in authSessions table
- ✅ JWT stored in browser localStorage

What Doesn't Work:
- ❌ getAuthUserId(ctx) returns
null

- ❌ ctx.auth._token is undefined (JWT not sent with requests)

Code:

Frontend (with useConvexAuth):
const { isAuthenticated, isLoading } = useConvexAuth();
const userProfile = useQuery(api.users.getCurrentUserProfile, isAuthenticated ? {} : "skip");


Backend:
export const getCurrentUserProfile = query({
  handler: async (ctx) => {
    const userId = await getAuthUserId(ctx);  // Returns null!
    if (userId === null) return null;
    return await ctx.db.get(userId);
  },
});


Logs:
[createOrUpdateUser] Returning user ID: 'k578y1k2dpbn09n6d3m725hcfs7tcx0f' ✅
[auth:store] type: verifyCodeAndSignIn ✅
[getCurrentUserProfile] userId: null ❌


What We've Tried:
- Implemented useConvexAuth() to wait for isAuthenticated: true before calling queries
- Verified JWT exists in localStorage
- Cleared all sessions and signed in fresh multiple times
- Used getAuthUserId() from @convex-dev/auth/server

Our auth.config.ts has domain: process.env.CONVEX_SITE_URL which may be undefined. Could this be blocking JWT validation? How should we properly configure authentication?
Was this page helpful?