How to globally configure an access token on every fetch api using plain fetch api ?
Unanswered
Barbary Lion posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Barbary LionOP
am have installed next auth and configured everything which is working fine. am connecting next auth to an extenal api which returns a response with access token.
my next auth route .ts
What is the best way to globally add access token from next auth session to every request ?
my next auth route .ts
import NextAuth, { AuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
const authOptions: AuthOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "email", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const url =
${process.env.BACKEND_BASE_URL}/auth;
const res = await fetch(url, {
method: "POST",
body: JSON.stringify({
email: credentials?.email,
password: credentials?.password,
}),
headers: {
"Content-Type": "application/json",
},
});
if (!res.ok) return null;
return (await res.json()) ?? null;
},
}),
],
callbacks: {
jwt: async ({ token, user }) => {
if (user) token = user as unknown as { [key: string]: any };
return token;
},
session: async ({ session, token }) => {
session.user = { ...token } as any;
return session;
},
},
session: {
strategy: "jwt",
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
What is the best way to globally add access token from next auth session to every request ?