Giter Site home page Giter Site logo

next-auth-protection's Introduction

Next Auth Protection ๐Ÿ”

Authentication protection for Next.JS pages and routes, powered by NextAuth.js ๐Ÿš€

  • Protect API routes
  • Protect pages
  • Protect pages before rendering

Table of contents

Documentation

To read the full documentation, visit this document.

Features

  • Fastest way to add authentication protection: with just a few lines of code you can protect every page and API route of your application!
  • Almost zero initial configuration: you just need to pass your already created NextAuth.js configuration object, and you're ready to go! next-auth-protection will use that config to optimize the verification process.
  • Flexible and compatible with every NextJS & NextAuth.js applications: works with every provider, adaptar and database supported by NextAuth.js.

Installation

To start using Next Auth Protection, install the package:

# npm
npm install next-auth-protection

# yarn
yarn add next-auth-protection

Usage

To start using Next Auth Protection, follow these steps:

  1. Initialize NextAuth.js ๐Ÿ”Œ

Make sure you have at least one provider set up and working in your project and you're wrapping your application with the <SessionProvider> component (needed in the package's page protector).

  1. Create your WithAuthProtection instance ๐Ÿ›ก๏ธ

Using your NextAuth.js configuration object, create a new instance of WithAuthProtection:

import { WithAuthProtection } from 'next-auth-protection';

const authProtection = new WithAuthProtection(NEXTAUTH_OPTIONS);
  1. Protect your routes ๐Ÿ”

You can protect your routes in three ways:

const authProtection = new WithAuthProtection(NEXTAUTH_OPTIONS);

// 1 - protect pages directly
const MyPage = authProtection.page(() => {
  return <h1>My page</h1>;
});

// 2 - protect pages before rendering
export const getServerSideProps = authProtection.getServerSideProps(() => {
  return {
    props: {
      message: 'Hello world',
    },
  };
});

// 3 - protect API routes
const handler = authProtection.api(async (req, res) => {
  res.status(200).json({ message: 'Hello world' });
});
  1. Optional: customize the redirect behavior ๐Ÿšฆ

Common Problems

next-auth-protection's People

Contributors

llapenna 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.