Jarrymi
Jarrymiβ€’2mo ago

Auth don't work with Next js and ConvexAuthNextjsProvider

I'm having issues with getting auth to work with the ConvexAuthNextjsProvider. All iss setup as described in https://labs.convex.dev/auth/authz/nextjs. In the middleware.ts it seems to be working as expected (isAuthenticated is true). But when i do await fetchAction(api.stripe.checkout, { plan: 'starter' }) and
export const checkout = action({ args: { plan: v.string() }, handler: async ({ auth }, { plan }) => { console.log('server identity', await auth.getUserIdentity()) }, }) It just return null (on a clickhandler insiden react).. I have gone trought the debugging section in the docs (https://docs.convex.dev/auth/debug) Everything looks good, except that in the header, i don't get typ: "JWT", only "alg": "RS256" Can you point me in the right direction?
Debugging Authentication | Convex Developer Hub
Troubleshoot authentication issues in Convex
No description
No description
No description
46 Replies
Jarrymi
JarrymiOPβ€’2mo ago
I'm using Oauth with google
Sara
Saraβ€’2mo ago
ello there, for serverside to work, you need to pass the auth token in your Action/Query/Mutation
Sara
Saraβ€’2mo ago
(to work for functions that require auth!) also, I prefer using the function
const userId = await getAuthUserId(ctx);
const userId = await getAuthUserId(ctx);
its just a personal preference since it already does the identity check
Jarrymi
JarrymiOPβ€’2mo ago
The component itself uses 'use client'
Jarrymi
JarrymiOPβ€’2mo ago
No description
Jarrymi
JarrymiOPβ€’2mo ago
I still need to do that?
Sara
Saraβ€’2mo ago
you don't need to use use client no but if you want you can pass "use server" at the top of the function defention
Jarrymi
JarrymiOPβ€’2mo ago
No, i mean, i want to use "use client". Then sending token is not necessary?
Sara
Saraβ€’2mo ago
if you want it to be client, use useAction/ useQuery oh hold on
Jarrymi
JarrymiOPβ€’2mo ago
Hm, so i need to use useAction, even if its inside a click handler?
Sara
Saraβ€’2mo ago
move the query out of the button let's do this from the start, shall we?
Jarrymi
JarrymiOPβ€’2mo ago
That seems to work
Sara
Saraβ€’2mo ago
Amazing haha
Jarrymi
JarrymiOPβ€’2mo ago
So i can't use fetchQuery directly inside a handler?
Sara
Saraβ€’2mo ago
no, because Queries are supposed to be used to fetchData, not to update them
Jarrymi
JarrymiOPβ€’2mo ago
I mean actions
Sara
Saraβ€’2mo ago
you can!
Jarrymi
JarrymiOPβ€’2mo ago
Or any of them really
Sara
Saraβ€’2mo ago
you just use useAction or fetchAction depending on what you really need
Jarrymi
JarrymiOPβ€’2mo ago
So why didn't my first example work?
Sara
Saraβ€’2mo ago
because you were using fetchQuery..
Jarrymi
JarrymiOPβ€’2mo ago
This don't work
No description
Jarrymi
JarrymiOPβ€’2mo ago
Then the user is null
Sara
Saraβ€’2mo ago
Again, this is because you didn't pass the token in the action :ahhh:
Jarrymi
JarrymiOPβ€’2mo ago
But its on the client So fetchAction on the client don't work?
Sara
Saraβ€’2mo ago
This is what I'm saying fetchAction is for server side Do you understand the difference, I can explain it if you want
Jarrymi
JarrymiOPβ€’2mo ago
Yeah, i think so πŸ™‚ @Sara Thank you, seems to be working. Another question i have when using Next, is the use of http.ts. Will tht work with the use of Next? I'm trying to get stripe callbacks to work, but it seems like when i follow the template (https://www.convex.dev/templates/stripe), /stripe is never acceccable (probably because its a Next route)?
Sara
Saraβ€’2mo ago
so when it comes to HTTP, you're not calling the api from Next, you'd be using the .site url from convex
Jarrymi
JarrymiOPβ€’2mo ago
`stripe listen --forward-to https://localhost:3000/stripe`` So this needs to be .site url, not localhost?
Sara
Saraβ€’2mo ago
no that seems to be correct if you want to do a http fetch request, you'd need to use the convex url I'm reading the Repo now
Jarrymi
JarrymiOPβ€’2mo ago
I'm talking about the callback that recives stripe events http.route({ path: '/stripe', method: 'POST', handler: httpAction(async (ctx, request) => { const signature: string = request.headers.get('stripe-signature') as string const result = await ctx.runAction(internal.stripe.webhook, { signature, payload: await request.text(), }) if (result.success) { return new Response(null, { status: 200, }) } else { return new Response('Webhook Error', { status: 400, }) } }), }) When i go to localhost:3000/stripe i get to a 404, because it don't exist in Next
Sara
Saraβ€’2mo ago
Yep this acts as an API request do you have a folder directory for stripe page?
Jarrymi
JarrymiOPβ€’2mo ago
So i need to do the stripe callback in the route.ts (in next), and do api calls for each event? I can crete a /stripe route in Next, and then do api calls to convex, but was hoping i didn't need the next route
Sara
Saraβ€’2mo ago
mmm I need some time to read the repo, I'm also not experienced with stripe so I can answer the questions
Jarrymi
JarrymiOPβ€’2mo ago
I'm pretty sure that the http.route /stripe is soemthign that stripe hits when doing callbacks As a webhook
Sara
Saraβ€’2mo ago
you could try that
Jarrymi
JarrymiOPβ€’2mo ago
I did try it, but as i said its not hit, because /stripe is served by next, not convex
Sara
Saraβ€’2mo ago
the route would be the url from convex, remove .cloud and replace it with .site
Jarrymi
JarrymiOPβ€’2mo ago
stripe listen --forward-to **.site I'll tryt his
Sara
Saraβ€’2mo ago
it seems we were wrong https://stack.convex.dev/stripe-with-convex I'm reading this right now, you should keep the hosting url
Wake up, you need to make money! (Add Stripe to your product)
If you’re building a full-stack app, chances are you’ll want some of your users to pay you for the service you provide. How to use Stripe with Convex ...
Jarrymi
JarrymiOPβ€’2mo ago
It worked with what i suggested above, to use the .site as forward-to Thanks
Sara
Saraβ€’2mo ago
Really! damn
Jarrymi
JarrymiOPβ€’2mo ago
I tried with localhost again now, and it actually seems that that also worked now πŸ˜… Not sure what i did wrong earlier
Sara
Saraβ€’2mo ago
that's pretty interesting good job
Jarrymi
JarrymiOPβ€’2mo ago
Anyways, thanks

Did you find this page helpful?