Git Product home page Git Product logo

nextjs-auth-hoc's Introduction

nextjs-auth-hoc

NPM version npm download

A Higher Order Component for restricting page access.

Installation

nextjs-auth-hoc

// with npm
npm install nextjs-auth-hoc

// with yarn
yarn add nextjs-auth-hoc

Configuration

Before using you have to specify some variables in .env of your project:

# Default page to redirect users if user is not authenticated
REDIRECT_IF_NOT_AUTHENTICATED=/auth/signin

# Default page to redirect if user is authenticated
REDIRECT_IF_AUTHENTICATED=/dashboard

# Default page to redirect if action is not authorized
REDIRECT_IF_NO_ACCESS=/dashboard

# Name of cookie key for JWT token storage
JWT_COOKIE_NAME=jwt

# Host of site, only need if your authentication happens on other domain
# It needed to pass "ref" query
REFERER=http://example.com

# Base URL of root endpoint
API_HOST=http://api.example.com/v1

# URL to get user session
# if not specified default will be "/auth/session"
GET_SESSION_URL=/user/profile

Usage

Here is a quick example to get you started, it's all you need:

import React from 'react';
import { Auth } from 'nextjs-auth-hoc';

class Posts extends React.Component {
    static async getInitialProps() {
        return {};
    }
    
    render() {
        const { user: { token } } = this.props // You also can access user object
        return (
            <div>List of posts</div>
        );
    }
}
export default Auth({ action: 'RINA' })(Posts)

There is also a special HOC withUser to access user object, it's all you need:

import React from 'react';
import { withUser } from 'nextjs-auth-hoc';

const Header = (props) => {	    
    return (
        <div>{props.user.name}</div>
    )
}
export default withUser(Header)

You can restrict accessing page by passing ACL option, it's all you need:

import React from 'react';
import { Auth } from 'nextjs-auth-hoc';

class Dashboard extends React.Component {
    static async getInitialProps() {
        return {};
    }
    
    render() {
        const { user: { token } } = this.props // You also can access user object
        return (
            <div>List of posts</div>
        );
    }
}
export default Auth({ action: 'RINA', ACL: ['admin'] })(Dashboard)

API

action (optional)

type: "string"

value description
RINA Redirect if not authenticated
RIA Redirect if authenticated

ACL (optional)

type: "array"

nextjs-auth-hoc's People

Contributors

hackerart avatar

Watchers

James Cloos 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.