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

react-use-session

v1.2.0

Published

useSession hook for React

Downloads

198

Readme

👤

react-use-session

React hook to handle session from your browser storage

Usage

Getting session

Import useSession from react-use-session and pass a sessionKey.


import { useSession } from  'react-use-session';

  

const { session } = useSession('my-app');

  

If that key is found in your browser storage then its value will be returned as session since your app first render. If not, the returned value is null.

Saving or clearing session

To save a new session you use the save function, you can save any object or string, to clear the session you use the clear function, both functions are given to you by useSession.


const { session, save, clear } = useSession('my-app');

Non-persistent session

If you don't want the session to be kept when the user closes the window. There is a second parameter called "keepOnWindowClosed" that you can assign to false. The default value is true, so session is persistent by default.

const { session } = useSession('my-app', false);

Auto "JSON Web Token" Parsing

If you have a string containing a JSON Web Token, instead of calling save, you can call saveJWT, which will automatically parse the JWT to a Javascript Object, save it in the browser local storage, and return the object back to you.


const { saveJWT } = useSession('my-app');

Example

import  React  from  'react';
import { useSession } from  'react-use-session';

function  App() {

const  JSON_WEB_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZWFjdC11c2Utc2Vzc2lvbi1leGFtcGxlIiwiaWF0IjoxNTQxMDgwMjAwLCJleHAiOjE5MTk3Njg0MDAsImF1ZCI6ImxvY2FsaG9zdDozMDAwIiwic3ViIjoiZ2FicmllbGJiMDMwNkBnbWFpbC5jb20iLCJHaXZlbk5hbWUiOiJHYWJyaWVsIiwiU3VybmFtZSI6IkJhc2lsaW8gQnJpdG8iLCJSb2xlIjoiQ3JlYXRvciJ9.GK23QsdEgMzGmxCwX9CjEg5lbSztZ7C67vKc7L09KgI";

const { session, saveJWT, clear } = useSession('jwt-test-app');

return (
<div  className="App">
{
session ?
<div>
<p>You are logged in as: <code>{session.GivenName}</code></p>
<button  onClick={clear}>Log out</button>
</div>
:
<div>
<p>No session. Please log in</p>
<button  onClick={() =>  saveJWT(JSON_WEB_TOKEN)}>Log in</button>
</div>
}
</div>)
}

export  default  App;

Screenshot

Code Documentation

/**

* React hook to handle session from your browser storage

* @param  sessionKey This required parameter is used as the browser storage key. This should be a unique string per app. For example your package.json's name value.

* @param  keepOnWindowClosed This optional parameter will determine if useSession will work with LocalStorage or SessionStorage. Default value is true, so default storage is LocalStorage

* @returns If there is a session in your browser storage then it will be returned. If there is no session, it will return null. It also returns 3 functions to handle the session

*/

declare  function  useSession(sessionKey:  string, keepOnWindowClosed?:  boolean): {

session:  object  |  string  |  null;

save: (sessionValue:  object  |  string) =>  void;

saveJWT: (jwt:  string) =>  void;

clear: () =>  void;

};
`