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

161
Visualizações
Check user that can view my files in my server

I want to secure my files and not anyone can view my files in express, I want to check who has logged in into my website and then authenticate if he has access to view this file or not , how can I achieve that this is the code I am using to access my files url :

app.use("/profile", express.static(__dirname + '/profile'));

I want only the user that logged in, he is the only one that can view his profile image and if he is not the user then say for example you are not allowed to view this file.

I am using mongodb as a backend but i don't know if its mongodb authentication or express middleware security ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I Discovered how to solve this :

const checkImg = async (req, res, next) => {
const token = req.cookies.jwt;
if (token) {
    jwt.verify(
        token,
        process.env.JWTKEY,
        async (err, decodedToken) => {
            if (err) {
                res.json({ status: false });
            } else {
                const user = await UserModal.findById(decodedToken.id);
                const imgUrl = req.originalUrl.split('/')[2];
                const imgUser = user.profileImg;
                if (imgUrl === imgUser || user.admin === "ADMIN") {
                    next();
                }
                else {
                    return res.send("Your are not allowed to view this file")
                }
            }
        }
    );
}else{
return res.send("you are not allowed !")
}

}

and here is when i use my middleware :

app.use("/images", checkImg , express.static(__dirname + '/images'));

this code checks if the user is the same user then he can only see the data and also the admin user can display the data , otherwise send him that he is not allowed to get the data

over 4 years ago · Santiago Trujillo Relatório
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