Can access to protected page such as admin.html in address bar

Hello,

I have an issue. I can access to my admin.html and client .html in address bar while I put a login and everything works fine when clicking on the links but in the address bar I wanted to test and unfortunately I also access it. How to correct the problem? Thanks for your help here is the code :

In the back end in app.js :


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

// Route pour le logout
app.get('/logout', (req, res) => {
    // Supprime les cookies
    res.clearCookie('loggedIn');
    res.clearCookie('isAdmin');
    res.clearCookie('isClient');

    // Redirige vers la page de connexion
    res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
    res.redirect('/login.html');
});

// Route pour la connexion d'un utilisateur
app.post('/login', async (req, res) => {
    const { username, password } = req.body;

    if (!username || !password) {
        return res.status(400).json({ message: "Veuillez fournir un nom d'utilisateur et un mot de passe." });
    }

    const query = "SELECT * FROM users WHERE username = ? AND password = ?";
    try {
        const [rows, fields] = await pool.execute(query, [username, password]);

        if (rows.length === 0) {
            return res.status(401).json({ message: "Nom d'utilisateur ou mot de passe incorrect." });
        }

        const user = rows[0];
        const isAdministrateur = user.administrateur === 1;
        const isClient = user.client === 1;

        // Set cookies for logged in user
        res.cookie('loggedIn', true);
        res.cookie('isAdmin', isAdministrateur);
        res.cookie('isClient', isClient);

        if (isAdministrateur) {
            return res.redirect('/admin.html');
        } else if (isClient) {
            return res.redirect('/client.html');
        } else {
            return res.status(401).json({ message: "Ce compte n'est pas autorisé à se connecter." });
        }
    } catch (error) {
        console.error(error);
        res.status(500).send("Une erreur est survenue lors de la connexion de l'utilisateur.");
    }
});

// Middleware pour vérifier si l'utilisateur est authentifié
function isAuthenticated(req, res, next) {
    if (req.cookies.loggedIn) {
        next();
    } else {
        res.redirect('/login.html');
    }
}

// Middleware pour vérifier si l'utilisateur est un administrateur
function isAdmin(req, res, next) {
    if (req.cookies.isAdmin) {
        next();
    } else {
        res.redirect('/login.html');
    }
}

// Middleware pour vérifier si l'utilisateur est un client
function isClient(req, res, next) {
    if (req.cookies.isClient) {
        next();
    } else {
        res.redirect('/login.html');
    }
}


// Route pour la page client.html avec middleware isClient
app.get('/client.html', isAuthenticated, isClient, (req, res) => {
    res.sendFile(path.join(__dirname, 'client.html'));
});

// Route pour la page admin.html avec middleware isAdmin
app.get('/admin.html', isAuthenticated, isAdmin, (req, res) => {
    res.sendFile(path.join(__dirname, 'admin.html'));
});

Hello,

Here is the github link :

Thank for your help

Are you sure you don’t just have a cookie set? Did you try clearing your cookies first, or test it in an Incognito window?

I clear the cache browser and i tested with incognito tab and nothing. I can always access from the adress bar

As far as I can tell it is because of how the static files are served. If you move the protected routes up before the static file serving it should work (I think).

Ok thank you I’ll try Great contributor

I moved routes before static file and I’ve got an error in both cases. That solution doesn’t run. Sorry ! But thanks for your answer

Saying you got an error doesn’t tell us anything and it doesn’t mean it didn’t work. What error are you getting?

We do not know how you changed the code. Show the code change or push the code (you can create a new branch for it if needed).

If you move the protected routes to before the static serving does the navigation to the admin file not trigger the middleware?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.