Admin login 500 Error, secretOrPrivateKey must have a value

Hey!

I deployed a Strapi backend, Vue frontend project on DigitalOcean via NGINX.
The project is “empty”, meaning it is the default boilerplate generated with the CLI.

The problem is with Strapi Admin login, i got an error after submitting.

error Error: secretOrPrivateKey must have a value
node_modules/strapi-admin/services/token.js:32:14
node_modules/strapi-admin/controllers/authentication.js:35:46

debug POST /admin/login (165 ms) 500

Browser console error: VM9:1 POST http:///api/admin/login 500 (Internal Server Error)

The Strapi version is 3.2.5, i did not migrate from previous versions whatsoever.

Here is my /etc/nginx/sites-available/default config

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name _;

        location / {
                root /var/www/html/blog/client/dist;
        }

        location /api/ {
                rewrite ^/api/(.*)$ /$1 break;
                proxy_pass http://strapi;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_pass_request_headers on;
        }

        location /dashboard {
                proxy_pass http://strapi/dashboard;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
                proxy_pass_request_headers on;
        }
}

Upstream is configured to localhost.

And the Strapi server.js:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'http://<dropletip>/api',
  admin: {
    url: '<dropletip>/dashboard',
    secret: env('ADMIN_JWT_SECRET')
  },
});

I did set up a .env file next to server.js and inside the root containing the generated ADMIN_JWT_SECRET, but is this enough? I mean, it’s not included in my requests.

System Information
  • Strapi Version: 3.2.5
  • Operating System: Ubuntu 20.04 (LTS) x64
  • Database: none
  • Node Version: v12.19.0
  • NPM Version: 6.14.8
  • Yarn Version: 1.22.5

It should not be included in requests. It is used only in backend.

From that one I see that you are missing the hostname. Or you deleted it from log?

Note that you should always rebuilt your Admin UI if you modify admin configs in server.js

  • as I remember, admin.url should contain only path, in you case 'url':'/dashboard', replace it.
  • delete .cache/build folders and run yarn build

It would appear that a secret key is not being passed to this environment variable, in your deployment how are you setting environment variables (specifically this one ADMIN_JWT_SECRET)

That error is basically saying that ADMIN_JWT_SECRET is undefined.

1 Like

I changed the server.js file a bit as sunnyson suggested:

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  url: 'http://167.99.243.1/api',
  admin: {
    url: '/dashboard',
    secret: 'krr/BQpkW18rZB+kjULCJVt7qI2E1Cop6JvQINxRwZD4N9kIL0QLufaa8VUzPror/QWQsxxJWsBjEYp9wp9zRw=='
  },
});

Also I hardcoded the JWT secret value.

Cleared the .cache and build folders, rebuilt with yarn build, but no luck I got the same secretOrPrivateKey error after the login attempt.

I found the solution.

The secret key must be inside an auth object in server.js. Everything loads jut fine, thank you guys for the inspiration!

2 Likes

Hi, just adding this here because it wasn’t clear for me and I had to struggle a bit with this: what I did was change the server.js to have it like this:
{url: process.env.HEROKU_URL,
admin: {
auth: {
secret: ${process.env.JWT_KEY}
}
},
and it worked.

1 Like