Giter Site home page Giter Site logo

simon-ince / nextjs-django-auth-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeffroche/nextjs-django-auth-example

0.0 1.0 0.0 122 KB

Example of integrating Next.js with a Django API

Home Page: https://nextjs-django-auth-example.vercel.app

Python 49.99% TypeScript 48.05% JavaScript 1.61% CSS 0.35%

nextjs-django-auth-example's Introduction

nextjs-django-boilerplate

A barebones example of a Next.js SPA backed by a Django API.

Includes the following:

Backend:

  • Django
  • Django REST Framework
  • JWT Authentication

Frontend:

  • Next.js
  • Tailwind

Setting up the backend API

Create and activate a virtualenv:

$ python3 -m venv .venv
$ source .venv/bin/activate

Install Python requirements:

$ pip install -r requirements/base.txt

Configure the Django environment:

  • Rename the sample environment file to .env:
    $ mv .env.sample .env
    
  • Edit the .env file and provide a value for SECRET_KEY

Set up the DB (uses sqlite by default):

$ python manage.py makemigrations api
$ python manage.py migrate

Running the API locally

$ python manage.py runserver 4001

The API is now running at http://localhost:4001

Setting up the frontend UI

In a new shell instance, switch to the www folder and install JavaScript dependencies:

$ cd www
$ npm install

Running the UI locally

$ npm run dev

The UI is now running. Visit http://localhost:4000 in your browser.

Running tests

$ python manage.py test

Deployment

Below is a quick overview on deploying the app to Heroku and Vercel.

Notes on securing cookies

This project is configured so that the Next.js app and Django API are deployed separately. Whether they are deployed to different subdomains on the same second level domain (so something like Next.js -> www.example.com, Django -> api.example.com) or completely separate domains will affect how the refresh token cookie settings should be configured. This is because the former configuration results in requests that are considered same-site which allows us to set the SameSite attribute in the cookie to Lax. Otherwise, we need to set the SameSite to None.

Backend

To deploy the backend on Heroku:

  1. Create a new app on Heroku
  2. Add Heroku Postgres
  3. Connect the app to your github repo
  4. Update the config variables (see below)
  5. On the Deploy tab in Heroku, trigger a deploy manually from Github (or switch on automatic deploys if you want).

Backend config vars

  • SECRET_KEY: see Django docs
  • DATABASE_URL: set automatically when Postgres is added
  • CORS_ORIGIN_REGEX_WHITELIST: A comma-separated list of origins (ref). This should include the URL that the Next.js app gets deployed to (see below).
  • IGNORE_DOT_ENV_FILE=on

Frontend

To deploy the frontend on Vercel:

  1. Click "Import Project"
  2. Enter the URL of your github repo
  3. Select the www subdirectory.
  4. Add the NEXT_PUBLIC_API_HOST env var with the value set to the URL the Django API gets deployed to
  5. Complete the build

nextjs-django-auth-example's People

Contributors

bmoeskau avatar jeffroche avatar

Watchers

 avatar

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.