Home > Enterprise >  Express API | Heroku - JWT (error - secret or private key must have a value)
Express API | Heroku - JWT (error - secret or private key must have a value)

Time:12-29

I deployed my API to Heroku and when I try to use it (login). I get some errors.

my code -

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const cors = require("cors");

// IMPORT FILES

const user = require("./routes/user");

// MONGOOSE CONNECTION
mongoose.connect(
  "CONNECTION URL HERE",
  { useNewUrlParser: true },
  () => {
    console.log("Connected to DB");
  }
);

// MIDDLEWARE CONFIGS
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
mongoose.Promise = global.Promise;

// ROUTES REDIRECTION

app.use("/", user);

// SERVER
const server = app.listen(process.env.PORT || 8080, () => {
  const port = server.address().port;
  console.log(`Server is running on port ${port}`);
});

JWT FILE

const jwt = require("jsonwebtoken");

module.exports = function (req, res, next) {
  const token = req.header("token");
  if (!token) return res.status(401).send("please login again");

  try {
    const verify = jwt.verify(token, "JWT SECRET HERE");
    req.user = verify;
    next();
  } catch (err) {
    res.status(400).send("invalid token");
  }
};

Some pictures of errors-

Error in Heroku logs --tails

error is browser

PS: i had .env for the variables but took 'em out for delpoyment's sake as I was getting Application error. thank you! help is appreciated.

CodePudding user response:

Have you added your environment variables to heroku config vars?

If not then follow the heroku documentation to do so.

https://devcenter.heroku.com/articles/config-vars

You can use the heroku cli or use the heroku dashboard to add the variables. Refer the docs for a detailed explanation.

  • Related