0

My logout doesn't work and I'm trying to figure out why.

When I click on submit it says : Cannot POST /logout

I have used passport to check for data from the users and I have create route for each webpage.

I have tried many things but I think the problem may come from the form on the action side.

Thank you for your help.

Here the code :

animator.ejs

<h1>Hi <%= name %></h1>
<form action="logout?_method=DELETE" method="POST">
    <button type="submit">Log Out</button>
</form>

animator.js

const express = require('express')
const router = express.Router()
const passport = require('passport')
require('../passport-config')(passport);

router.get('/', (req, res) =>{
    res.render('animator/animator', {name: req.user.email})
})

router.delete('/logout', (req, res) =>{
    req.logout()
    res.redirect('/login')
})

module.exports = router

server.js

if(process.env.NODE_ENV !== 'production') {
const dotenv = require('dotenv')
dotenv.config();
}

const express = require('express')
const expressLayouts = require('express-ejs-layouts')
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
const dotenv = require('dotenv')

const app = express()

require('./passport-config')(passport);

const indexRouter = require('./routes/index')
const registerRouter = require('./routes/register')
const loginRouter = require('./routes/login')
const parentRouter = require('./routes/parent')
const animatorRouter = require('./routes/animator')

app.set('view engine','ejs')
app.set('views', __dirname + '/views')
app.set('layout', 'layouts/layout')

app.use(expressLayouts)
app.use(express.static('public'))
app.use(express.urlencoded({ extended : false }))
app.use(flash())
app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())


const mongoose = require('mongoose');

mongoose.connect(process.env.DATABASE_URL, {
useNewUrlParser: true, useUnifiedTopology: true}).then(()=>{
    console.log('Successfully connected to the mongoDB Atlas!')
}).catch((error)=>{
    console.log('impossible to connect to the mondoDB Atlas !')
    console.error(error);
});

app.use('/', indexRouter)
app.use('/register', registerRouter)
app.use('/login', loginRouter)
app.use('/animator', animatorRouter)
app.use('/parent', parentRouter)


app.listen(process.env.PORT || 3000)

2 Answers2

2

It looks like you are making a POST request, but the route is configured for DELETE. You can either update the form HTML to use the DELETE method or configure POST for the route.

Form Update Option:

<form action="/logout" method="DELETE">

Server Reconfiguration Option:

router.post('/logout', (req, res) =>{
    req.logout()
    res.redirect('/login')
})
<form action="/logout" method="POST">
jasonandmonte
  • 1,869
  • 2
  • 15
  • 24
  • I think it's better to change it to `POST` from server-side, instead. – omidh May 19 '21 at 21:40
  • Do you know a better solution it doesn't work ? – Mohamed Necib May 20 '21 at 15:27
  • Can you explain the error or issue you see? Are you still getting `Cannot POST /logout` or is it something new? – jasonandmonte May 20 '21 at 17:45
  • Ok I found the answer thank you for you help :) On the file index.js I put this code : `router.post('/logout', (req, res) => { req.logOut() res.redirect('/login') })` and then after that on parent and animator ejs pages I just wrote this code `

    Hi <%= name %>

    ` and it worked :)
    – Mohamed Necib May 22 '21 at 13:59
  • You're welcome! I would recommend for future questions you mark the answer that helped you as accepted. Rather than adding your own implementation and marking your own answer. – jasonandmonte May 22 '21 at 15:46
0

I did find an answer to my problem.

Thank you for your help.

Here is what I did :

on a file called index.js

  router.post('/logout', (req, res) => {
  req.logOut()
  res.redirect('/login')
})

and then on the file parent.js and animator.js I

<h1>Hi <%= name %></h1>
<form action="/logout?_method=DELETE" method="POST">
    <button type="submit">Log Out</button>
</form>

And everything worked very fine.