Cannot POST /authenticate

Asked

Viewed 44 times

-1

inserir a descrição da imagem aqui

I wonder if you could help me, give it to you yesterday with that mistake I no longer know what to do.

index js.

const express = require("express");
const app = express();
const port = 3000;
const connection = require("./database/database");
const userController = require("./user/UserController");
const User = require("./user/User");
const bodyParser = require("body-parser");
const session = require("express-session");
const router  = express.Router()



app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.set('view engine', 'ejs');

//static
app.use(express.static('public'));

//session
app.use(session({
    secret: "dshjodfiajoiashashjdaas", cookie: {maxAge: 300000}
}));



app.get("/", (req, res) => {
    res.render("index");

});




//routes use

app.use("/", userController);


// data base

connection
    .authenticate()
    .then(() => {
        console.log("Sucess banco de dados");
    })
    .catch((msgErro) => {
        console.log(msgErro);
    })




app.listen(port, () => {
    console.log("Serve On")
});

Usercontroller.js

const express = require("express");
const router  = express.Router();
const User = require("./User");
const bcrypt = require('bcryptjs');


router.get("/home", (req, res) =>{
    res.render("home/home");
})

router.get("/error", (req, res) => {
    res.render("users/usuarioNaoExite");
});

router.get("/erro", (req, res) => {
    res.render("loginErro");
});

router.get("/registro", (req, res) => {
    res.render("users/registrar");
});

router.post("/registro/users", (req, res) => {
    let email = req.body.email;
    let senha = req.body.senha;


    User.findOne({where: {email: email}}).then( user => {
        if( user == undefined){
            let salt = bcrypt.genSaltSync(10);
            let hash = bcrypt.hashSync(senha, salt);

            User.create({
                email: email,
                senha: hash
            }).then(() => {
                res.redirect("/");
            }).catch((erro) => {
                console.log("Erro da table");
            });
        
            
        }else {
            res.render("users/registrarErro");
        }
    })

   // rota de autenticação de usuario
    router.post("/authenticate", (req, res) => {
        let email = req.body.email;
        let senha = req.body.senha;

        User.findOne({where: {email: email}}).then(user => {
            if(user != undefined){ // se existe um usario com esse  email
                //valida  a senha
                let correct = bcrypt.compareSync(senha, user.senha);

                if(correct){
                    req.session.user = {
                        id: user.id,
                        email: user.email
                    }
                    res.redirect("/");
                }else{
                    res.redirect("/");
                }


            }else{
                res.redirect("/")
            }
        });

    });

});


module.exports = router;

index ejs.

<%- include ('partials/header.ejs') %>
<body>
    <h1>olá</h1>
    <form action="/authenticate" method="POST">
        <input type="email" name="email">
        <input type="password" name="senha">
        <button type="submit">Logar</button>
        </form>
        <form action="/registro" method="GET">
            <button type="submit">Registrar</button>
        </form>
       
</body>
<%- include ('partials/footer.ejs') %>

1 answer

-2


Your file is ill-endowed.

In case the solution was to remove the route /authenticate from within the route /registro/users.

Follows the solution:

const express = require("express");
const router  = express.Router();
const User = require("./User");
const bcrypt = require('bcryptjs');


router.get("/home", (req, res) =>{
    res.render("home/home");
})

router.get("/error", (req, res) => {
    res.render("users/usuarioNaoExite");
});

router.get("/erro", (req, res) => {
    res.render("loginErro");
});

router.get("/registro", (req, res) => {
    res.render("users/registrar");
});

router.post("/registro/users", (req, res) => {
    let email = req.body.email;
    let senha = req.body.senha;


    User.findOne({where: {email: email}}).then( user => {
        if( user == undefined){
            let salt = bcrypt.genSaltSync(10);
            let hash = bcrypt.hashSync(senha, salt);

            User.create({
                email: email,
                senha: hash
            }).then(() => {
                res.redirect("/");
            }).catch((erro) => {
                console.log("Erro da table");
            });
        
            
        }else {
            res.render("users/registrarErro");
        }
    })
})

   // rota de autenticação de usuario
router.post("/authenticate", (req, res) => {
        let email = req.body.email;
        let senha = req.body.senha;

        User.findOne({where: {email: email}}).then(user => {
            if(user != undefined){ // se existe um usario com esse  email
                //valida  a senha
                let correct = bcrypt.compareSync(senha, user.senha);

                if(correct){
                    req.session.user = {
                        id: user.id,
                        email: user.email
                    }
                    res.redirect("/");
                }else{
                    res.redirect("/");
                }


            }else{
                res.redirect("/")
            }
    });
});


module.exports = router;
  • Thank you very much face, it was at five o'clock in the morning and I saw that kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.