/next-auth-hasura-boilerplate

:art: Boilerplate for building applications using Hasura and Next.js

Primary LanguageTypeScriptMIT LicenseMIT

Logo

Twitter: Nirmalya Ghosh

This is a boilerplate for building applications using Hasura and Next.js. This boilerplate consists of the following:

  1. frontend: Next.js application
  2. backend: Dockerized Hasura application

Overview

This boilerplate is built using the following technologies:

  1. Chakra UI
  2. Emotion
  3. GraphQL
  4. urql
  5. NextAuth
  6. TypeScript

It supports GraphQL Query, Mutation and Subscription out of the box.

Requirements

  1. Node.js
  2. npm
  3. Docker

Packages

1. Frontend: Next.js application

This application is the primary user-facing application. Once it’s up and running (see Development section), it’s available on http://localhost:3000/.

Home page before login

To create a new user, we’ll have to Sign Up using Google. NextAuth is being used to help us in authentication. Login page

Home page after login

Feeds page

My Profile page

2. Backend: Dockerized Hasura application

Hasura is an open source engine that connects to our databases & micro-services and auto-generates a production-ready GraphQL backend. It’s very easy to get Hasura up and running on our local system. All the migrations are set up in the migrations directory.

Installation

1. Clone the application

git clone https://github.com/ghoshnirmalya/nextjs-hasura-boilerplate

2. Install necessary dependencies for the frontend application

cd frontend && yarn install

3. Create a .env file and copy the contents from .env.example (present in frontend directory)

4. Generate the RSA keys

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout > public.pem

5. Print the keys in the escaped format

awk -v ORS='\\n' '1' private.pem

6. Copy the value of the key into the AUTH_PRIVATE_KEY key (in the .env file)

The AUTH_PRIVATE_KEY should look like the following:

AUTH_PRIVATE_KEY='{"type":"RS256", "key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA0U3NR0eyMehHBlx6DK5sJ+Pys9dTWf558kpKVeQvL2oIZEY3\nLvS3/jdrIm/dU4WrIGPki1r/AWXQAyBZ2FKnZROcUWN0IqdmxrC5zTDymuscqhKX\nqxjSwrwOVWHc+zWWmXCQGmDdoCokXd9ZW66nA0BN66MdMC2+d5GrZdKUF305dpzT\nUdzDE12/XwOEUalCM0703eGu8zFwutLYc3+vf2CFOQ1z+rv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6NF7VNiyC8GP0ILJazfRrnjp7cou5Nav0pxyVHQniIq3wihD39irNbK16BDZ25Bj\nQ/1C+Qzing2VNvCnwEwHKpkOMrigZB1N6VSmFdIvwNNmrRoQMcIKvr5ZBY1GE/Bn\nfR53AoGBAIXaWIoDW5d9XwFa8HdxkgMPyLlizckZKyXASYEGWD2VU8P1NwA/bZ1t\nymioQPRJymTBfUL6E44Ebwx25DezjYEun1yqouZ+WZBlsEYtssffzTs2IocZ6aCN\nYfzt3orUEI/rWbRSqYFEuOntzzf3a7r3MtDU41e7iXcNkRSxCAIV\n-----END RSA PRIVATE KEY-----\n"}'

7. Create and copy the Google client credentials

Create a new Google OAuth Client and copy the credentials (Client ID and Client Secret) in your .env file.

8. Start the frontend application

From the frontend directory, we can run the following command to start our Next.js frontend application:

yarn dev

The above command will start the frontend application on http://localhost:3000/.

9. Go inside the directory of the backend package on another terminal window

cd packages/backend

10. Add the necessary environment keys (in the docker-compose.yml file)

awk -v ORS='\\n' '1' public.pem

The HASURA_GRAPHQL_JWT_SECRET should look like the following (in your docker-compose.yml file):

HASURA_GRAPHQL_JWT_SECRET='{"type":"RS256", "key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0U3NR0eyMehHBlx6DK5s\nJ+Pys9dTWf558kpKVeQvL2oIZEY3LvS3/jdrIm/dU4WrIGPki1r/AWXQAyBZ2FKn\nZROcUWN0IqdmxrC5zTDymuscqhKXqxjSwrwOVWHc+zWWmXCQGmDdoCokXd9ZW66n\nA0BN66MdMC2+d5GrZdKUF305dpzTUdzDE12/XwOEUalCM0703eGu8zFwutLYc3+v\nf2CFOQ1z+rvDQD4N2aZABKTxZRtEkMHljnoyKlF9rljNzT/5N8YQE7qn4pBh6CMa\n1zcSilk9nhgl55n/Kjn2xMieWdIalaOEKw1LqqIjiT1ESkAKfPaIoSSnmTaYy78g\nbwIDAQAB\n-----END PUBLIC KEY-----\n"}'

Also, the HASURA_GRAPHQL_ADMIN_SECRET value needs to be updated in the docker-compose.yml file:

HASURA_GRAPHQL_ADMIN_SECRET: secret

11. Start docker-compose

docker-compose up

We need to start Docker and then run the above command which will change the current directory to the backend package’s directory and then start the backend package. If everything goes well, it’ll be up and running on http://localhost:8080/v1/graphql.

Deployment

Frontend application

Click on the button below to deploy the frontend application on Vercel. You'll need to sign up for a free Vercel account.

Deploy with Vercel

Backend application

Click on the button below to deploy the backend application on Heroku. You'll need to sign up for a free Heroku account.

Deploy to Heroku

Other interesting repositories

  1. Hasura Next.js Boilerplate
  2. React Search Box
  3. LinkedIn Clone using Create React App

License

This project is licensed under the MIT License.