#How to access logged in user details in next js server side?
49 messages · Page 1 of 1 (latest)
?
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. ```import { NextRequest, NextResponse } from "next/server";
import { getAuth } from "@clerk/nextjs/server";
import { NextApiRequest, NextApiResponse } from "next";
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { userId } = getAuth(req);
if (!userId) {
console.log("not found", userId);
return res.status(401).json({ error: "Not authenticated" });
}
// Process the webhook data
const webhookData = req.body;
console.log("webhook data=================================>", userId);
res.status(200).json({ status: true, webhookData });
}
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
import { httpAction } from "./_generated/server";
const http = httpRouter();
http.route({
path: "/paddle",
method: "POST",
handler: httpAction(async (ctx, request) => {
const requestBody = await request.json();
const webhookData = requestBody.data;
console.log(
"webhook data=================================>",
webhookData,
"request body==>",
requestBody
);
if (webhookData) {
return new Response(null, {
status: 200,
});
} else {
return new Response("Webhook Error", {
status: 400,
});
}
}),
});
export default http;
``` 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 am using paddle.js, https://www.paddle.com/
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
Yeah I am using the sdk
See if you can get the transaction id from the checkout.loaded event
They 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
You'll want to pass a callback to Paddle.Initialize(), details in that doc ^^
Ohh I see