This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

next-simple-auth
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

next-simple-auth

Simple cookie authentication system for Next.js

  • Uses cookies 🍪
  • Written in Typescript
  • Supports server-side rendering ⚡️
  • Can be used with OAuth bearer tokens / GraphQL / REST apis etc.
npm install next-simple-auth --save

Usage

Configuration:

import createAuth from 'next-simple-auth';

const auth = createAuth({
  cookieExpirySeconds: 86400 * 365, // 1 year
  cookieName: 'access_token',
  https: true,
  getContext(accessToken: string | null) {
    return {
      // Make the API available to every page
      api: new Api(accessToken),
    };
  },
});
export default auth;

Then in your pages:

import auth from 'src/auth';
export default MyPage(props: { content: string }) {
  return <div>{props.content}</div>;
}

auth.bind(MyPage, async ({ api }) => {
  return {
    content: await api.doSomething(),
  };
});

Hooks:

// pages/_app.tsx
import App, { AppContext } from 'next/app';
import auth from 'src/auth';

export default class MyApp extends App<{}> {
  static async getInitialProps(appContext: AppContext) {
    const session = auth.get(appContext.ctx);

    try {
      return App.getInitialProps(appContext);
    } catch (err) {
      if (err.status === 401) {
        auth.redirectToLogin(appContext.ctx);
        return {};
      }
    }
  }
  render() {
    const { Component, pageProps, router } = this.props;

    return (
      <auth.Provider>
        <Component {...pageProps} />
      </auth.Provider>
    );
  }
}

// pages/example.tsx
import auth from 'src/auth';
export default function MyComponent() {
  const session = auth.useSession();
  function onClick() {
    session.api.post('/users/me/images', { x: 32 });
  }
  return <button onClick={onClick}>Do something</button>;
}

For a full example see /example.

/next-simple-auth/

    Package Sidebar

    Install

    npm i next-simple-auth

    Weekly Downloads

    12

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    9.53 kB

    Total Files

    5

    Last publish

    Collaborators

    • aantthony