@agderposten/user-provider

2.3.5 • Public • Published

User Provider

HOC that provides the user object to its children when given a token. Attempts to use Next.js getInitialProps. You probably need @babel/polyfill for this to work.

Installation

npm install @agderposten/user-provider

Usage

Preferably use this HOC only once and on the root component.

import React from 'react';
import userProvider from '@agderposten/user-provider';

export default () => userProvider({
    token: 'enfwueg7f8g2uibf2fg79we8ohi23bufivygh8ow'
}, ({user}) => (
    <div>Hello, {user.name.fullName}</div>
));

// ... or implicit, where it tries to read the user agent token

export default () => userProvider(({user}) => (
    <div>Hello, {user.name.fullName}</div>
));

You can also provide an optional LoaderComponent as the last function argument that will be rendered when loading user information:

export default () => userProvider(({user}) => (
    <div>Hello, {user.name.fullName}</div>
), () => (
    <div>Loading user information...</div>
));

user

The user object looks something like this when logged in:

{
    isLoggedIn: true,
    customerNumber: 123456,
    email: 'example@email.com',
    name: {
        full: 'Some Name',
        first: 'Some',
        last: 'Name'
    }
}

... and like this when logged out:

{
    isLoggedIn: false,
    error: {
        code: 'unauthorized',
        message: 'Unauthorized (unauthorized): invalid signature'
    }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @agderposten/user-provider

Weekly Downloads

0

Version

2.3.5

License

UNLICENSED

Unpacked Size

12.6 kB

Total Files

4

Last publish

Collaborators

  • frank.bjelland
  • gaute
  • thomaslindstr_m