beingkartik
beingkartik12mo ago

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.
const { userId } = getAuth(req);
const { userId } = getAuth(req);
I am getting userId null here
33 Replies
Devben
Devben12mo ago
Convex Clerk | Convex Developer Hub
Clerk is an authentication platform providing login via
beingkartik
beingkartikOP12mo ago
?
Devben
Devben12mo ago
Oh! sorry the doc explain everything to get started using convex with clerk
beingkartik
beingkartikOP12mo ago
I have already done that, thats not my question.
erquhart
erquhart12mo ago
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.
beingkartik
beingkartikOP12mo ago
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 });

}
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 });

}
I am trying to get the auth data like this
erquhart
erquhart12mo ago
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)
beingkartik
beingkartikOP12mo ago
Can we get user details inside a http request in convex?
erquhart
erquhart12mo ago
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
beingkartik
beingkartikOP12mo ago
import { httpRouter } from "convex/server";
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;
import { httpRouter } from "convex/server";
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.
erquhart
erquhart12mo ago
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.
beingkartik
beingkartikOP12mo ago
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
erquhart
erquhart12mo ago
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
beingkartik
beingkartikOP12mo ago
Yes they are authenticated, I am using clerk with convex
erquhart
erquhart12mo ago
Okay, so you can get their userid the regular way when they first initiate the payment process - why is that not an option?
beingkartik
beingkartikOP12mo ago
But the payment response I am getting through webhooks. This is how I am handling the payment response
erquhart
erquhart12mo ago
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?
beingkartik
beingkartikOP12mo ago
Correct
erquhart
erquhart12mo ago
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?
beingkartik
beingkartikOP12mo ago
Yes I have user table
erquhart
erquhart12mo ago
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.
beingkartik
beingkartikOP12mo ago
ahh that's not possible I guess, I'll get the transaction id only when I complete the payment
erquhart
erquhart12mo ago
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
erquhart
erquhart12mo ago
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
erquhart
erquhart12mo ago
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.
beingkartik
beingkartikOP12mo ago
Yeah I am using the sdk
erquhart
erquhart12mo ago
See if you can get the transaction id from the checkout.loaded event
beingkartik
beingkartikOP12mo ago
They only have three events on sdk I guess
No description
erquhart
erquhart12mo ago
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.
erquhart
erquhart12mo ago
You'll want to pass a callback to Paddle.Initialize(), details in that doc ^^
beingkartik
beingkartikOP12mo ago
Ohh I see
erquhart
erquhart12mo ago
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)

Did you find this page helpful?