Can anyone help me with refreshing the Firebase access token when it expires? I need the token for API authentication, but I can't find any information online regarding next-auth and Firebase.
Currently, I am able to retrieve the access token but struggling to refresh it upon expiration. This leads to an API authentication error after some time.
import { auth } from "@/config/firebaseApp";
import { signInWithEmailAndPassword } from "firebase/auth";
import { AuthOptions } from "next-auth";
import NextAuth from "next-auth/next";
import CredentialsProvider from "next-auth/providers/credentials";
const authOptions: AuthOptions = {
pages: {
signIn: "/signIn",
},
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
authorize: async (credentials): Promise<any> => {
return await signInWithEmailAndPassword(
auth,
(credentials as any).email || "",
(credentials as any).password || ""
)
.then(async (userCredentials) => {
const dummy = {
role: "admin",
};
if (userCredentials.user) {
return {
...userCredentials.user,
role: dummy.role || "user",
access_token: await userCredentials.user.getIdToken(),
};
}
return null;
})
.catch((err) => {
console.log(err.message);
});
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.role = user.role;
token.access_token = user.access_token;
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.role = token.role;
}
return session;
},
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
I'm new to this, so please let me know if there is anything wrong in my approach.