How to access logged in user details in next js server side?
I am trying to get the logged in user details in server side in next js.I am using clerk for authentication. I am getting userId null here
33 Replies
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
?
Oh! sorry the doc explain everything to get started using convex with clerk
I have already done that, thats not my question.
Where is
getAuth()
defined/imported?
ah that's a clerk method
It's tough to troubleshoot from just that one line. Is there a session id in the auth object? Are you sure the user is signed up and authenticated? If you could share more context it would help.Yes user is signed in. I am trying to get the auth data like this
Is the request authenticated though? If it's an inbound webhook I would assume not
For webhooks you generally use verification rather than authentication - if the webhook is verified you can act on the contents
(Assuming it's a webhook based on the function there)
Can we get user details inside a http request in convex?
An inbound request isn't inherently associated with a user or session. So you'll want to find a way to connect the request to the user based on the data in the request.
But once you do know which user an inbound request pertains to, you can fetch the user with a query. You'll often need queries to determine which user that is in the first place.
I can help ideate a bit if you're able to share more of your use case
I am using paddle payment gateway and paddle is hitting this endpoint as webhook, So I want to store payment info to user who made the payment. So for that I need to get the user id who is currently logged in.
Just looking briefly at their docs, there's a transaction id for each payment - I haven't looked thoroughly, but I'm guessing you'll want to grab that transaction id as soon as it's available and store it in your Convex database, along with the relevant user id. When the webhook comes back after the payment has completed, you can verify the webhook and then use the transaction id to get the user info.
Consider that a rough approach, details will vary depending on how Paddle payments work and how your app is set up.
I am also thinking the same but the issue is how should I reference the payment id with user id
I mean I don't have access to user id
When the user goes into the flow to start a payment in your app, are they authenticated?
Or is the payment not being initiated in your app
Yes they are authenticated, I am using clerk with convex
Okay, so you can get their userid the regular way when they first initiate the payment process - why is that not an option?
But the payment response I am getting through webhooks.
This is how I am handling the payment response
So you have an authenticated user, that user initiates a payment in your app, and when that process is complete, a webhook comes back from paddle, correct?
Correct
You can create something like a
paddle_transaction
table, it will take a paddle transaction id and a convex user id. I'm assuming you have a users table in your Convex database, is that correct?Yes I have user table
You'll need to find a point in the payment flow when you have the authenticated user in your app, the user has engaged the payment flow, and you have a paddle transaction id, but before they submit/complete the payment.
At that point, before they've completed the flow, you create a document in
paddle_transaction
with the paddle transaction id and the currently authenticated user id.
Then, when a webhook comes in for a transaction, you can use the paddle transaction id to look up the associated record in paddle_transaction
. That record will have a user id, which you can use to look up the user that submitted the payment.
You won't be using any kind of authentication when handling the inbound webhook, you're just using the transaction id to find the user id and fetching the user from the db for whatever purpose you may have.ahh that's not possible I guess, I'll get the transaction id only when I complete the payment
Are you sure?
I'm looking in paddle's docs and they emit events during the process
Are you using paddle.js?
There's also Paddle Billing and Paddle Classic, do you know which one you're using
I don't know how you're using their sdk, but if you're creating a Payment, you get a transaction id via emitted event as soon as they select a payment method: https://developer.paddle.com/paddlejs/payment/checkout-payment-selected
Actually, when checkout is first loaded you get a transaction id in the loaded event: https://developer.paddle.com/paddlejs/general/checkout-loaded
checkout.loaded - Paddle Developer
We provide everything you need to sell software in one integration, with advanced solutions to help you grow and optimize your software business.
Yeah I am using the sdk
See if you can get the transaction id from the
checkout.loaded
eventThey only have three events on sdk I guess

Those are methods - here's the overview on handling events: https://developer.paddle.com/paddlejs/events/overview
Events - Paddle Developer
Learn about the events emitted by Paddle.js as customers move through the checkout.
You'll want to pass a callback to
Paddle.Initialize()
, details in that doc ^^Ohh I see
There's also a
customer.created
event - I don't know how customer creation works in Paddle or whether you can expect a given Paddle customer to be tied to a given authenticated user, but if such a connection can be made, you could connect webhooks to users via paddle customer id instead of transaction id. But that may just complicate things more (eg., handling multiple customer ids per user id)