Capture login data to store on my own Postgresql Db using efcore

Hello,

I am using nextjs in my frontend and we would like to store the information captured in the authentication process (in nextjs) on our own database. I have followed this tutorial: Auth0 Next.js SDK Quickstarts: Add Login to your Next.js application and this has worked for us, however I am still not sure how to store the information gathered through this method (email in this case and the associated Auth0Id) on our own database.

I guess the most efficient way of doing this is to let asp.net core capture the user data without retrieving it in the frontend and then send it to the backend (as this would take 2 requests instead of 1 when just capturing it in my backend with asp.net core). I just don’t know how to implement this in my own stack, because I don’t see the relevant information on the Auth0 docs.

Hi @mierlo.paul , welcome to Auth0!

Once a user logged in, Auth0 ships ID token and Access tokens (access token is later consumed by your API).

These two tokens are supplied with the user ID (under the sub claim) that you can store in your database. To add user email to them you can use Auth0 actions (to add the custom claim with the email address of the user that logs in).

Below is a preview of decoded with jwt.io sample access token where I already added a custom claim with the user email:

Hope this helps, let us know!

1 Like

Thank you for your reply,

What I don’t yet understand is how to retrieve the access token. Is in my case the best way to retrieve the access token through nextjs and then send it to my asp.net core backend? In that case I already tried to retrieve the access token in nextjs, but to no avail unfortunately.

import React, { useEffect } from "react";
import { getAccessToken } from "@auth0/nextjs-auth0";
import { useUser } from "@auth0/nextjs-auth0/client";

export default function AccessToken() {
  const { user, error, isLoading } = useUser();

  useEffect(() => {
    const handleGetUserInfo = async () => {
      try {
        const accessToken = await getAccessToken();
        const response = await fetch(
          "https://dev-nndt467y4bl6m363.us.auth0.com/api/v2/",
          {
            headers: {
              Authorization: `Bearer ${accessToken}`,
            },
          }
        );

        if (response.ok) {
          console.log("test");
          const userData = await response.json();
          console.log("User Data:", userData);
          // Handle user data as needed
        } else {
          console.error("Failed to fetch user information");
        }
      } catch (error) {
        console.error("Error fetching user information:", error);
      }
    };

    if (user) {
      handleGetUserInfo(); // Call handleGetUserInfo() when the user is logged in
    }
  }, [user]); // Run the effect whenever the user object changes (login/logout)
}

Whenever I login as a user this code is not executed at all I think. The way I implemented it right now is that I used the nextjs Quickstarts Login: Auth0 Next.js SDK Quickstarts: Login

Hi @mierlo.paul ,

In your code snippet it looks like you only request the access token:

but you do not return the object (in Javascript function means) to be able to handle it later.

Can you please adjust your code snippet and let us know?