2

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'),
  },
});

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

why in this video he doesn't use it too youtube.com/watch?v=F2rQPEeL9FU&t=341s 7:20
and if I do that I got new error Missing apiToken.salt. Please set apiToken.salt in config/admin.js (ex: you can generate one using Node with crypto.randomBytes(16).toString('base64')).
ok I just added API_TOKEN_SALT and ADMIN_JWT_SECRET else and all work
0

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")
}

Comments

0

To resolve the issue create a .env file which should be a copy of .env.example. Depending on your version of Strapi you may need to add more environment variables. Then use Node in the terminal to run crypto.randomBytes(16).toString('base64') commands, after running the command you will get a generated key to use in the .env

Here's an example of a working .env file, just swap generatedKey with your crypto keys.

HOST=0.0.0.0
PORT=1337
APP_KEYS=['generatedKey1', 'generatedKey2']
JWT_SECRET=generatedKey3
API_TOKEN_SALT=generatedKey4
ADMIN_JWT_SECRET=generatedKey5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.