Where to generate next auth secret for next auth
Asked Answered
U

6

19

The documentation for NextAuth.js says that I can generate a secret for next auth by entering this command:

openssl rand -base64 32

Here is the documentation: https://next-auth.js.org/configuration/options#nextauth_secret

However, when I enter the command in the terminal within VS code I get this error. Error given in VS code when I run the command in file path:  PS C:\nextjs\Amazon-starter-template-nextjs>

I'm trying to run this command on Windows.

Unrighteous answered 4/1, 2023 at 3:17 Comment(0)
E
22

Just use Bash terminal in VS code. You can create new terminal: enter image description here

And then choose Bash: enter image description here

And just run script in Bash

PS: You can have multiple terminals open at once which is helpful sometimes

Eiser answered 28/2, 2023 at 16:3 Comment(1)
From the screenshot: openssl rand -base64 32Launcelot
R
5

Paste this in your Vscode terminal or Gitbash or whichever terminal you are using:

npx auth secret

Then make sure you add the new secret to your .env file:

AUTH_SECRET="put your auto generated secret";
Rodroda answered 8/2, 2024 at 4:48 Comment(0)
C
3

I know this is a few months old but I still want to answer the question.

Its because by default OpenSSL is not installed onto Windows machines, the command doesn't work on CMD or Powershell.

Git Bash comes with OpenSSL and hence the command will work on Git Bash which is why it'll work if you open it on VS code like in Grayza's answer.

EDIT: I have just discovered today that you might also need to add winpty before the openssl command in Git Bash. Found through this post In Git-Bash-Windows `openssl pkcs12` does not prompt for password

If you do have it installed on your machine then it might not be in your system's environment variables.

Conlon answered 15/6, 2023 at 23:47 Comment(0)
P
1

If you use Vercel for your hosting, you can generate a secret at https://generate-secret.vercel.app/32

Perlis answered 13/11, 2023 at 14:17 Comment(1)
You can use that for any hosting.Clymer
A
0

To set up NEXTAUTH_SECRET for NextAuth.js, follow these steps:

  1. Generate the Secret Key: Open your terminal and run:
    openssl rand -base64 32
    
    

This will output a secure, base64-encoded string.

  1. Add the Secret to .env.local: Add the generated key to your .env.local file:

    .env

    NEXTAUTH_SECRET=your_generated_secret_here
    
    

Replace your_generated_secret_here with the actual key.

Amherst answered 23/7, 2024 at 7:52 Comment(0)
Y
0

Considering you are using Windows, you can simply install the openssl tool in your Windows Terminal using Chocolatey: choco install openssl. Then, you'd follow the steps mentioned in NextAuth/Authjs documentation:

  1. Generate the secret key with openssl rand -base64 32.
  2. Set either NEXTAUTH_SECRET or AUTH_SECRET (the latest one if you are using the latest NextAuth.js v5) on your .env file.

Remember that you'll need set an environment variable in a similar fashion whenever you decide to deploy your application. You can generate a random key in a similar fashion.

Youngs answered 6/8, 2024 at 3:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.