auth/auth-mw.js
// mw to protect a route, check that user is auth'd using firebase
mw to protect a route, check that user is auth'd using firebase
mw to protect a route check that user is auth ' d using firebase
async function protect(req, res, next) {
  const idToken = req.headers.authorization;
  if (process.env.DB_ENV === "testing") {
    if (idToken === "motherToken") {
      req.user = { user_id: "mother0FIREBASE" };
    } else if (idToken === "driverToken") {
      req.user = { user_id: "driver0FIREBASE" };
    } else {
      req.user = { user_id: "test" };
    }
    return next();
  }
  if (idToken) {
    admin
      .auth()
      .verifyIdToken(idToken)
      .then(decodedIdToken => {
        req.user = decodedIdToken;
        return next();
      })
      .catch(error => {
        console.error("Error while verifying Firebase ID token:", error);
        res.status(403).send("Unauthorized");
      });
  } else {
    res.status(401).json({
      message: "Log in and provide token to view this content."
    });
  }
}
async function protect ( req , res , next ) { const idToken = req . headers . authorization ; if ( process . env . DB_ENV === "testing" ) { if ( idToken === "motherToken" ) { req . user = { user_id : "mother0FIREBASE" } ; } else if ( idToken === "driverToken" ) { req . user = { user_id : "driver0FIREBASE" } ; } else { req . user = { user_id : "test" } ; } return next ( ) ; } if ( idToken ) { admin . auth ( ) . verifyIdToken ( idToken ) . then ( decodedIdToken => { req . user = decodedIdToken ; return next ( ) ; } ) . catch ( error => { console . error ( "Error while verifying Firebase ID token:" , error ) ; res . status ( 403 ) . send ( "Unauthorized" ) ; } ) ; } else { res . status ( 401 ) . json ( { message : "Log in and provide token to view this content." } ) ; } }
mw to protect a route, check that user is auth'd using firebase
mw to protect a route check that user is auth ' d using firebase
// console.log(idToken); // for testing // was firebaseId supplied? // verify: // verify ok // console.log("ID Token correctly decoded", decodedIdToken);