Hello World
Hello World

Reputation: 15

How to config passport-jwt module? I have an error: Login sessions require session support

Hey there!

I'm starting to learn Node.js courses and in one of it the guy making a registration and authentication with MEAN stack. But his code is old and not working today with updated modules. But I fixed alot of bugs (thx to youtube comments) but when I testing http requests I still have an error:

Error: Login sessions require session support. Did you forget to use `express-session` middleware?

BUT Google says This module lets you authenticate endpoints using a JSON web token. It is intended to be used to secure RESTful endpoints without sessions.

There is index file:

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');

// Connect To Database
mongoose.connect(config.database);

// On Connection
mongoose.connection.on('connected', () => {
  console.log('Connected to Database '+config.database);
});

// On Error
mongoose.connection.on('error', (err) => {
  console.log('Database error '+err);
});


const app = express();
const users = require('./routes/users');

// Port Number
const port = process.env.PORT || 3030;

// CORS Middleware
app.use(cors());

// Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));

// Body Parser Middleware
app.use(bodyParser.json());

// Passport Middleware
app.use(passport.initialize());
app.use(passport.session());

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

app.use('/users', users);

// Index Route
app.get('/', (req, res) => {
  res.send('invaild endpoint');
});

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'public/index.html'));
});

// Start Server
app.listen(port, () => {
  console.log('Server started on port '+port);
});

Upvotes: 1

Views: 170

Answers (1)

add {session: false} in route to be like that passport.authenticate('jwt', { session: false })

Upvotes: 0

Related Questions