@breathecode/react-session

Create and mantain a sessions in react, compatible with React Router

Usage no npm install needed!

<script type="module">
  import breathecodeReactSession from 'https://cdn.skypack.dev/@breathecode/react-session';
</script>

README

Note: This package is experimental, I'm working on making it a lot lighter by removing some dependencies out of the bundle.

React Session Management

Create and maintain a sessions in react, compatible with React Router (login/logout) and sync with the localstorage

Installation

$ npm i --save @breathecode/react-session

Usage

Open a session by doing:

import {Session} from '@breathecode/react-session';

const token = '<any token that represents the session>';
const user = {
    username: 'alesanchezr',
    id: 2234234,
    //any other info you want to save on the session
}

Session.actions.login({ user: user, access_token: token });

Retrieve the session on any other moment

import {Session} from '@breathecode/react-session';
const session = Session.store.getSession();
//retrieve the user
console.log(session.user);
//retrieve the token
console.log(session.access_token);

Close the session by doing:

import {Session} from '@breathecode/react-session';

Session.actions.logout();

Make a Private Route using react router

The library brings a component called PrivateRoute to make your routs private automatically.

<BrowserRouter>
    <div>
        <Switch>
            <Route exact path='/login' component={Login} />
            <PrivateRoute exact path='/profile' component={PrivateLayout} />
        </Switch>
    </div>
</BrowserRouter>