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
1 Answer
Reset to default 7The 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
版权声明:本文标题:javascript - I want to send JWT token to my backend in next-auth - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744085355a2588415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论