NextJs Moralis Auth Saving users to database

hey guys im trying to save users to my database after authentication using the updated tutorial

NEXTAUTH

import NextAuth from 'next-auth';
import { MoralisNextAuthProvider } from '@moralisweb3/next';

export default NextAuth({
 
  providers: [
    MoralisNextAuthProvider(),
  ],

  callbacks: {
    async jwt({ token, user }) {
      if (user) {
        token.user = user;
      }
      return token;
    },
    async session({ session, token }) {
      session.user = token.user;
      return session;
    },
  },
});

moralis

import { MoralisNextApi } from '@moralisweb3/next';

export default MoralisNextApi({
  apiKey: process.env.MORALIS_API_KEY,

  authentication: {
    domain: 'google.com',
    uri: process.env.NEXTAUTH_URL,
    timeout: 120,
  },


  
});

in the previous version I was able to do so in this new version I cant seem to figure out where I should do the process

The previous version of the Next.js tutorial didn’t use a database unless you did this yourself after. What did you do? It shouldn’t be that different - NextAuth will still create a session and then you can do something with it.

1 Like

so in the previous version this is how I added the data to the database

export default NextAuth({
    providers: [
        CredentialsProvider({
            name: 'MoralisAuth',
            credentials: {
                message: {
                    label: 'Message',
                    type: 'text',
                    placeholder: '0x0',
                },
                signature: {
                    label: 'Signature',
                    type: 'text',
                    placeholder: '0x0',
                },
            },
            async authorize(credentials) {
                try {
                    const { message, signature } = credentials;

                    await Moralis.start({ apiKey: process.env.MORALIS_API_KEY });

                    const { address, profileId, expirationTime } = (await Moralis.Auth.verify({ message, signature, network: 'evm' })).raw;

                    let user = { address, profileId, expirationTime, signature, Verified: true };

                    const db = await connectDB();
                    console.log("connected to db");


                    //add the user to the DB





                    return user;


                } catch (e) {
                    // eslint-disable-next-line no-console
                    console.error(e);
                    return null;
                }
            },
        }),
    ],

i see that the authorize function was taking care of the auth with moralis and the output {address, profileId} was saved to the DB so basically I’m trying to figure out for the new version where should i do such actions

In this case you can just get the session from NextAuth - this code is already being used in the user.js file (from the tutorial):

import { getSession } from 'next-auth/react';

...

// in a function

const session = await getSession();
// connect to DB