#How to access logged in user details in next js server side?

49 messages · Page 1 of 1 (latest)

remote crystal
#

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

fringe sky
remote crystal
#

?

fringe sky
#

Oh! sorry the doc explain everything to get started using convex with clerk

remote crystal
#

I have already done that, thats not my question.

timber fern
#

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.

remote crystal
#

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

}

timber fern
#

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)

remote crystal
#

Can we get user details inside a http request in convex?

timber fern
#

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

remote crystal
#
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.
timber fern
#

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.

remote crystal
#

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

timber fern
#

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

remote crystal
#

Yes they are authenticated, I am using clerk with convex

timber fern
#

Okay, so you can get their userid the regular way when they first initiate the payment process - why is that not an option?

remote crystal
#

But the payment response I am getting through webhooks.

remote crystal
timber fern
#

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?

remote crystal
#

Correct

timber fern
#

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?

remote crystal
#

Yes I have user table

timber fern
#

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.

remote crystal
#

ahh that's not possible I guess, I'll get the transaction id only when I complete the payment

timber fern
#

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

timber fern
remote crystal
#

Yeah I am using the sdk

timber fern
#

See if you can get the transaction id from the checkout.loaded event

remote crystal
#

They only have three events on sdk I guess

timber fern
#

You'll want to pass a callback to Paddle.Initialize(), details in that doc ^^

remote crystal
#

Ohh I see