Failing to verify a firebase id_token on backend from NextAuth GoogleProvider NextJs 13
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
As the title states, I switched from using the standard Firebase Auth package and my own Auth Context Provider to the NextAuth method using GoogleProvider.
3 Replies
Asiatic LionOP
I get the following error where it says the verification method requires the Firebase Project ID but is receiving the OAuth2.0 Client ID.
detail: 'Authentication failure: Firebase ID token has incorrect "aud" (audience) claim. Expected "Firebase Project ID" but got "OAuth2.0 Client ID". Make sure the ID token comes from the same Firebase project as the service account used to authenticate this SDK. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve ID token.'Python Authentication Class:
Next Js Auth.js:
ewo_firebase_creds = credentials.Certificate(settings.EWO_FIREBASE_CONFIG)
ewo_firebase_app = initialize_app(ewo_firebase_creds, name="ewo_fb")
class EwoFirebaseAuthentication(JWTAuthentication):
def authenticate(self, request):
try:
header = self.get_header(request)
raw_token = self.get_raw_token(header)
except Exception as ex:
raise EwoException(f"No valid token: {ex}", 401)
try:
decoded_token = auth.verify_id_token(raw_token, app=ewo_firebase_app)
except Exception as ex:
raise EwoException(f"Authentication failure: {ex}", 401)
user_id = decoded_token["uid"]
look_up_info = {"userId": user_id}
return None, look_up_infoNext Js Auth.js:
export const authOptions = {
session: {
strategy: "jwt",
},
pages: {
signIn: "/auth/signin",
newUser: "/",
},
providers: [
GoogleProvider({
clientId: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
clientSecret: process.env.NEXT_PUBLIC_GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
user.id = account.id_token
return user
},
async redirect({ url, baseUrl }) {
return baseUrl
},
async session({ session, user, token }) {
session.user.token = token.sub;
return session
},
async jwt({ token, user, account, profile, isNewUser }) {
return token
}
},
};NextJs Page.js Axios call that generates error:
const session = await getServerSession(authOptions);
const token = session.user.token;
const url = `${process.env.NEXT_PUBLIC_PORTAL_API}is_registered`;
const getIsRegistered = await axios
.get(url, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((res) => {
console.log(res.data);
})
.catch((error) => {
console.log("this is error",error);
});