Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
Express js fix user authentication collision

I am making an app from express js, it uses firebase admin for authentication and I am also using custom jwt for user recognition, and mongodb for database.

I don't know the right term but usually happens rarely for eg, User A is editing his account, but for some reason User B's account gets edited.

I have made user authentication middleware.

import {
  Request,
  Response,
  NextFunction
} from "express";
import JSONRESPONSE from "../utils/JSONReponse";
import jwt from "jsonwebtoken"
import {
  User
} from "../models/User";

export async function AuthMiddleware(req: Request, res: Response, next: NextFunction) {
  const JSONResponse = new JSONRESPONSE(res);
  try {
    const session = req.cookies.session;
    if (!session) return JSONResponse.notAuthorized();
    const decoded = < any > jwt.decode(session);
    const uid = decoded.user_id
    if (!uid) return JSONResponse.notAuthorized();
    const user = await User.findOne({
      uid
    })
    if (!user) return JSONResponse.notAuthorized();
    req.app.locals.currentUser = user;
    req.app.locals.uid = uid;
    next()
  } catch (err) {
    console.log(err);
    JSONResponse.notAuthorized();
  }

}

Here is my main routes:

app.use("/api/auth", AuthRoutes)
app.use("/api/user", AuthMiddleware, UserRoutes)
app.use("/api/date", AuthMiddleware, DateRoutes)
app.use("/api/chat", AuthMiddleware, ChatRoutes)

How do I fix this issue, I am not sure what is causing this rare issue.

UPDATE:

https://stackoverflow.com/a/35111195/14786214

after looking at this answer. I think the problem was of req.app.locals since app.locals stores the variable throughout the lifecycle of app while res.locals stores only for the request lifecyle.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda