EN VI

Javascript - Firebase is saying that the ID doesn not match?

2024-03-12 07:30:03
Javascript - Firebase is saying that the ID doesn not match

I've made a website for my school, but I'm trying to expand on it. It's not that secure with auth and such, so I'm making a server for users to login, then when they want to update their settings on the website, it will send a request to the server to do so.

I'm currently trying to work on the user logging in. When I go to the "/login" route, it brings me to the Google login page. I login and it brings me to the "/app" route. In the console, I get the log below.

FirebaseAuthError: Firebase ID token has incorrect "aud" (audience) claim. Expected "school-progress-g" but got "1090170234957-5ntr48jf178o9befpp5n0603hoh7m2rh.apps.googleusercontent.com". 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 an ID token.
    at FirebaseTokenVerifier.verifyContent (/rbd/pnpm-volume/124c6272-9622-4f1d-925a-072a4f9e9ad7/node_modules/firebase-admin/lib/auth/token-verifier.js:239:19)
    at /rbd/pnpm-volume/124c6272-9622-4f1d-925a-072a4f9e9ad7/node_modules/firebase-admin/lib/auth/token-verifier.js:160:18 {
  errorInfo: {
    code: 'auth/argument-error',
    message: 'Firebase ID token has incorrect "aud" (audience) claim. Expected "school-progress-g" but got "1090170234957-5ntr48jf178o9befpp5n0603hoh7m2rh.apps.googleusercontent.com". 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 an ID token.'
  },
  codePrefix: 'auth'
}

I would also appreciate some help with how I would be able to continue developing this login system into my website, after the error is solved. Here are my code files:

server.js

const cookieParser = require("cookie-parser");
const express = require("express");
const admin = require("firebase-admin");

const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://school-progress-g-default-rtdb.firebaseio.com",
});

const PORT = process.env.PORT || 3000;
const app = express();

app.engine("html", require("ejs").renderFile);
app.use(express.static("static"));

app.use(cookieParser());

const firebaseAuthMiddleware = (req, res, next) => {
  const idToken = req.cookies.idToken || "";
  
  if (idToken == "") {
    return next();
  }

  admin
    .auth()
    .verifyIdToken(idToken)
    .then((decodedToken) => {
      req.user = decodedToken;
      next();
    })
    .catch((error) => {
      console.log(error);
      next();
    });
};
app.use(firebaseAuthMiddleware);

const { google } = require("googleapis");

const oauth2Client = new google.auth.OAuth2(
  "1090170234957-5ntr48jf178o9befpp5n0603hoh7m2rh.apps.googleusercontent.com",
  "XXXXXXXX (censored)",
  "https://scpr-server-version.glitch.me/callback"
);

app.get("/login", (req, res) => {
  const authUrl = oauth2Client.generateAuthUrl({
    access_type: "offline",
    scope: ["https://www.googleapis.com/auth/userinfo.email"],
  });
  res.redirect(authUrl);
});

app.get("/callback", async (req, res) => {
  const { tokens } = await oauth2Client.getToken(req.query.code);
  res.cookie("idToken", tokens.id_token);
  res.render("app.html");
  return false;
  res.redirect("/app");
});

app.get("/logout", (req, res) => {
  res.clearCookie("idToken");
  res.redirect("/login");
});

app.get("/app", (req, res) => {
  if (req.user) {
    res.render("app.html");
  } else {
    res.redirect("/login");
  }
});

app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});

I appreciate any help I can get! <3

I've tried changing the client ID in server.js with my Firebase ID, but it just causes more errors.

Solution:

I'm not entirely sure why you expect this system to work, but you seem to be missing one important detail: The Firebase Admin SDK can only verify user tokens for user accounts created by Firebase Authentication. Whatever you're doing with google.auth.OAuth2 for end users is just not compatible with Firebase.

If you want to sign in a user with their Google Account in a way that's compatible with Firebase Authentication, there are special instructions for doing that entirely in your frontend code. Once the user is signed in with Google and has a Firebase Auth user account linked to that Google account, only then you can verify the user's ID token with the Firebase Admin SDK (assuming that your frontend passes along the user ID token in the request.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login