Giter VIP home page Giter VIP logo

next-auth-hasura-example's Introduction

NextAuth.js + Hasura example

About this project

This is an example of how to use NextAuth.js library to add authentication to a Next.js application together with Hasura.

About NextAuth.js

NextAuth.js is an easy to implement, full-stack (client/server) open source authentication library designed for Next.js and Serverless.

Go to next-auth.js.org for more information and documentation.

NextAuth.js is not associated with Vercel or Next.js.

Getting started

1. Clone the repository and install dependancies

git clone https://github.com/nextauthjs/next-auth-example.git
cd next-auth-example
npm i

2. Configure your local environment

Copy the .env.local.example file in this directory to .env.local (which will be ignored by Git):

cp .env.local.example .env.local

Add details for one or more providers (e.g. Google, Twitter, GitHub, Email, etc).

Get those secrets right

Do not commit these secrets to git.

HS256

Go to https://generate-secret.now.sh/32 and generate a random secret. Put this secret in two places:

  • Create .env.local for Next.js

It should look something like this:

SECRET=<insert your secret here>
  • Create hasura-grahql-jwt-secret.json for Docker compose
{
    "type": "HS256",
    "key": "<insert your secret here>"
}

RS256

  • Generate private key
openssl genrsa -out private.pem 2048
  • Generate public key
openssl rsa -in private.pem -pubout -out public.pem
  • Transform public key to a single line, and copy to clipboard on Macos
awk -v ORS='\\n' '1' private.pem | pbcopy
  • Create .env.local for Next.js

It should look something like this:

In double quotes, otherwise the \n will be escaped, resulting in a PEM routines error

SIGNING_ALGORITHM=RS256
SECRET="<paste your secret here>"
  • Transform public key to single line, and copy to clipboard on Macos.
awk -v ORS='\\n' '1' public.pem | pbcopy
  • Create hasura-grahql-jwt-secret.json for Docker compose
{
    "type": "RS256",
    "key": "<paste your secret here>"
}

Running Hasura with docker compose

Remember the json file we created? We're going to put it to good use here.

Why though? Docker compose does not really like setting environment variables as json, for me this is - as of writing - the most convenient way to set HASURA_GRAPHQL_JWT_SECRET is the docker compose file.

HASURA_GRAPHQL_JWT_SECRET=$(cat hasura-graphql-jwt-secret.json) docker-compose up -d

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.