npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nextjs-auth-hoc

v1.0.5

Published

A Higher Order Component for restricting page access.

Downloads

27

Readme

nextjs-auth-hoc

NPM version

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"