Secure your local development server with HTTPS (Next.JS)

Sometimes you just need to secure your localhost in order to test some stuff, like OAuth for example.

Photo by Tommy Lee Walker on Unsplash

Create certificate

Creating a certificate for localhost is easy with . Just put the following command in the terminal. The output will be two files: localhost.key and localhost.crt

openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

Click on the crt file, on macOS the keychain app will open, add the key to it.

Now double click on it and under the trust section you will see “When using this certificate” select “Always Trust”.

Next.js

Now in Next.js we have to create our own server.js file if not already.

Run the server, you now have a secure connection to localhost.

developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store