@agderposten/user-provider
v2.3.5
Published
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.
Downloads
73
Readme
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: '[email protected]',
name: {
full: 'Some Name',
first: 'Some',
last: 'Name'
}
}
... and like this when logged out:
{
isLoggedIn: false,
error: {
code: 'unauthorized',
message: 'Unauthorized (unauthorized): invalid signature'
}
}