admin管理员组

文章数量:1397074

I am using next js. I am using next-auth for authentication. it is a MERN stack project.

Question: How can I get jwt token and send it to my backend using next-auth and axios.

(session.jwt) is giving me undefined.

here is my nextauth.js file:

import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import { MongoDBAdapter } from '@next-auth/mongodb-adapter';
import clientPromise from '../../../lib/mongodb';

export default NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
  callbacks: {
    session: async ({ session, user }) => {
      if (session?.user) {
        session.user.id = user.id;
      }
      return session;
    },
  },
  adapter: MongoDBAdapter(clientPromise),
  secret: process.env.JWT_SECRET,
  session: {
    jwt: true,
    maxAge: 30 * 24 * 60 * 60, // the session will last 30 days
  },
});

I am using next js. I am using next-auth for authentication. it is a MERN stack project.

Question: How can I get jwt token and send it to my backend using next-auth and axios.

(session.jwt) is giving me undefined.

here is my nextauth.js file:

import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import { MongoDBAdapter } from '@next-auth/mongodb-adapter';
import clientPromise from '../../../lib/mongodb';

export default NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
  callbacks: {
    session: async ({ session, user }) => {
      if (session?.user) {
        session.user.id = user.id;
      }
      return session;
    },
  },
  adapter: MongoDBAdapter(clientPromise),
  secret: process.env.JWT_SECRET,
  session: {
    jwt: true,
    maxAge: 30 * 24 * 60 * 60, // the session will last 30 days
  },
});
Share Improve this question asked Jan 18, 2023 at 6:04 Sajib HossainSajib Hossain 1232 silver badges13 bronze badges 1
  • Note: I am using only google authentication. – Sajib Hossain Commented Jan 18, 2023 at 6:36
Add a ment  | 

1 Answer 1

Reset to default 7

The example in their docs persists the provider (account) access token in the jwt callback and then includes it to the session object:

  callbacks: {
    async jwt({ token, account }) {
        if (account) {
          token.accessToken = account.access_token;
        }
        return token;
      },    
    async session({ session, token, user }) {
      if (session?.user) {
        session.user.id = user.id;
      }
      return {
        ...session,
        accessToken: token.accessToken
      };
    },
  }

However, if you want the raw JWT (not the provider access token) and you're server side you can use the getToken function:

export async function getServerSideProps(context) {
  const token = await getToken({ req: context.req, raw: true });
// ...
}

Then to use it in your requests, here in the authorization header for example):

const config = {
    headers : {
        'Authorization' : `Bearer ${token}`
    }
}
axios.get('http://web./api', config);

本文标签: javascriptI want to send JWT token to my backend in nextauthStack Overflow