cps-user3
cps-user39mo ago

Null Response from ctx.auth.getUserIdentity() in Next.js API

Hello. I am using Auth0 to verify user authentication. I have created a Next.js app and a Flutter app using the same Auth0 domain, and I am fetching data for the Flutter app from the Next.js API. Could you please suggest a good way to use await ctx.auth.getUserIdentity()? Currently, the result is always null. convex/users.ts export const getUser = query({ handler: async (ctx) => { const identity = await ctx.auth.getUserIdentity(); return identity } }); api/users/route.ts export async function GET(req: NextRequest) { const user = await fetchQuery(api.users.getUser); return NextResponse.json(user); }
9 Replies
ballingt
ballingt9mo ago
Have you gone through this guide? https://docs.convex.dev/auth/auth0
Convex & Auth0 | Convex Developer Hub
Auth0 is an authentication platform providing login via
cps-user3
cps-user3OP9mo ago
Yes, of course.Could the result of await ctx.auth.getUserIdentity(); be null because I am accessing Convex through the Next.js API from a Flutter app? If so, is there another way to pass the authentication token to the Convex backend?
ballingt
ballingt9mo ago
How are you accessing Convex? If it's with the HTTP client you'll need to call setAuth() on it with your JWT token.
cps-user3
cps-user3OP9mo ago
Thank you for answering! Next.js is accessing Convex in the following way. "use client"; import { ReactNode } from "react"; import { ConvexReactClient } from "convex/react"; import { ConvexProviderWithAuth0 } from "convex/react-auth0"; import { Auth0Provider } from "@auth0/auth0-react"; const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!); export default function ConvexClientProvider({ children, }: { children: ReactNode; }) { return ( <Auth0Provider domain={process.env.NEXT_PUBLIC_AUTH0_DOMAIN!} clientId={process.env.NEXT_PUBLIC_AUTH0_CLIENT_ID!} authorizationParams={{ redirect_uri: typeof window === "undefined" ? undefined : window.location.origin, }} useRefreshTokens={true} cacheLocation="localstorage" > <ConvexProviderWithAuth0 client={convex}> {children} </ConvexProviderWithAuth0> </Auth0Provider> ); } I am wondering if there is a way to use await ctx.auth.getUserIdentity() when a user logged into a Flutter app using the same Auth0 domain tries to call the Next.js app's API to fetch data. How can Convex recognize that the user is authenticated?
Michal Srb
Michal Srb9mo ago
Next.js Server Rendering | Convex Developer Hub
Next.js automatically renders both Client and Server Components on the server
cps-user3
cps-user3OP9mo ago
Thank you for the response! So, does it mean that when using 'httpAction' and passing jwtToken to headers, i can use 'ctx.auth.getUserIdentity();'? import { httpRouter } from "convex/server"; import { httpAction } from "./_generated/server"; const http = httpRouter(); http.route({ path: "/getIdentity", method: "GET", handler: httpAction(async (ctx, request) => { const identity = await ctx.auth.getUserIdentity(); return new Response(JSON.stringify(identity), { headers: { "content-type": "application/json", }, status: 200, }); }), }); export default http; If we only pass jwtToken to headers when invoking the 'httpAction' elsewhere, can i then use 'identity'?
Michal Srb
Michal Srb9mo ago
For HTTP actions these are the relevant docs: https://docs.convex.dev/functions/http-actions#authentication
HTTP Actions | Convex Developer Hub
HTTP actions allow you to build an HTTP API right in Convex!
cps-user3
cps-user3OP9mo ago
I tested the method you provided and it works well. Thank you so much!

Did you find this page helpful?