Session Secret Value Error

What’s happening:
I added a session secret value in the sample.env file on the REPL and specified a secret key on the session object in

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

But I still get an error:

express-session deprecated req.secret; provide secret option server.js:20:9

and then (after the message about the server starting on the given port number):

Error: secret option required for sessions
    at session (/home/runner/boilerplate-advancednode/node_modules/express-session/index.js:200:12)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/home/runner/boilerplate-advancednode/node_modules/body-parser/lib/types/urlencoded.js:91:7)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:317:13)
    at /home/runner/boilerplate-advancednode/node_modules/express/lib/router/index.js:284:7

My code so far

"use strict";
require("dotenv").config();
const express = require("express");
const myDB = require("./connection");
const fccTesting = require("./freeCodeCamp/fcctesting.js");
const session = require("express-session");
const passport = require("passport");

const app = express();

fccTesting(app); //For FCC testing purposes
app.use("/public", express.static(`${process.cwd()}/public`));

app.set("view engine", "pug");
app.set("views", "./views");

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

app.use(passport.initialize());
app.use(passport.session());

app.route("/").get((req, res) => {
  res.render(`${process.cwd()}/views/pug/index`, {
    title: "Hello", message: "Please login"
  });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);
});

This is only the code from server.js since aside from changing the quotes used, I haven’t touched the other files.
My browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42.

Challenge: Set up Passport

Link to the challenge:

Any help is appreciated. Thanks.

Hello there,

The sample.env file is not used by anything. Changing it does nothing. You need to work with a .env file. Or, for the case of Replit, you need to use the SECRETS feature described here:

If you are still confused, I suggest some research into what environment variables are, and how to use them in Node applications.

Hope this clarifies

Yeah, the first link helped. I managed to solve the challenge. Thanks.

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