Skip to content
Advertisement

Everything is fine but logout doesn’t work – (Cannot POST /logout)

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)

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement