Middleware "strapi::session": App keys are required. Please set app.keys in config/server.js (ex: keys: ['myKeyA', 'myKeyB'])

Viewed 1468

Trying to set up strapi and cloudinary I watched two videos and they are all OK, but I have an error, tell me why?

Middleware "strapi::session": App keys are required. Please set app.keys in config/server.js (ex: keys: ['myKeyA', 'myKeyB'])

enter image description here

plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'cloudinary',
      providerOptions: {
        cloud_name: env('CLOUDINARY_NAME'),
        api_key: env('CLOUDINARY_KEY'),
        api_secret: env('CLOUDINARY_SECRET'),
      },
      actionOptions: {
        upload: {},
        delete: {},
      },
    },
  },
  // ...
});

.env

HOST=0.0.0.0
PORT=1337
CLOUDINARY_NAME="my data"
CLOUDINARY_KEY="my data"
CLOUDINARY_SECRET="my data"
JWT_SECRET=my data

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
});

middlewares.js

module.exports = [
  'strapi::errors',
  'strapi::security',
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

admin.js

module.exports = ({ env }) => ({
  auth: {
    secret: env('ADMIN_JWT_SECRET'),
  },
  apiToken: {
    salt: env('API_TOKEN_SALT'),
  },
});
2 Answers

It looks like you don't have APP_KEYS defined in your .env file (assuming you're sharing the full file), as well as some other env variables you're accessing using strapi's built in env utility.

In your .env you should have a value for the following that should follow this format:

APP_KEYS=['key1','key2']

You can read more about environment config from Strapi's development docs: https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/optional/environment.html#strapi-s-environment-variables

I wrote a quick kotlin script to generate the necessary environment variables for Strapi. For security reasons, you should run this locally. But you can of course run it on play.kotlin.org. You can put the output of this script in your .env file.

import java.security.SecureRandom
import java.util.Base64

fun main() {

    val rnd = SecureRandom()

    val appKeys = List(4) {
        val buffer = ByteArray(16)
        rnd.nextBytes(buffer)
        Base64.getEncoder().encodeToString(buffer)
    }

    val buffer = ByteArray(16)
    rnd.nextBytes(buffer)
    var jwtSecret = buffer.joinToString("") { String.format("%02x", it) }
    jwtSecret = jwtSecret.substring(0, 8) + "-" + jwtSecret.substring(8, 12) + "-" + jwtSecret.substring(12, 16) + "-" + jwtSecret.substring(16)

    rnd.nextBytes(buffer)
    val apiTokenSalt = buffer.joinToString("") { String.format("%02x", it) }

    println("APP_KEYS=" + appKeys.joinToString(","))
    println("JWT_SECRET=$jwtSecret")
    println("API_TOKEN_SALT=$apiTokenSalt")
}

Related