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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-session-hook

v2.0.1

Published

ReactJS hook to manage session state and storage

Readme

Overview

Greenkeeper badge

Coming soon

Getting Started

Install react-session-hook using yarn:

yarn add react-session-hook

Or npm:

npm install --save react-session-hook

And within your react component:

import useSession, { UseSessionProvider } from 'react-session-hook';

const Component = () => {
  const session = useSession();

  const handleLogin = () => session.setSession({ token: newToken })
  const handleLogout = () => session.removeSession()

  if (session.isAuthenticated) {
    return (
      <div>
        <div>Logged in as: {session.profile.name}</div>
        <button onClick={handleLogout}>Logout</button>
      </div>
    )
  } else {
    return (
      <div>
        <div>You are logged out</div>
        <button onClick={handleLogin}>Login</button>
      </div>
    )
  }
}

export const App = () => (
  <UseSessionProvider>
    <Component />
  </UseSessionProvider>
)

Examples

See the examples folder

UseSessionProvider Options

{
  // The initial value for a token.
  // If undefined, they will default to the value in storage
  initialAccessToken: String (optional)
  initialIdToken: String (optional)
  initialRefreshToken String (optional)
  initialToken: String (optional)

  // The session profile.
  // If undefined, they will follow this logic
  //
  // If profileFn = True
  //    Set as the return value of profileFn
  // Else if jwt = True
  //    This will be set as the payload of the id Token or token
  initialProfile: Object (optional)

  // Attempt to parse profile and expiration date from the tokens
  jwt: Boolean (True)

  // Date object for the session expiration
  // If jwt = True
  //    Uses the 'exp' field in the accessToken or token body
  //    If there is no 'exp' field, this value is used
  // If no expiration is set the session will expire after 10 hours
  // Set value to null to explicitly set a session that never expires
  expiration: Date (optional)

  // Returned object passed to setSession
  // Notes:
  //   - will not be fired if isAuthenticated = false
  //   - will be fired before the refreshInterval
  //     if session expires before refresh period
  refreshFn: async (session) => Session (optional)

  // How often the refreshFn is called.
  refreshInterval: Number (1 * 60 * 60 * 1000) // 1 hours

  // Provide your own profile parsing logic. Useful for string tokens
  profileFn: (String) => Object | void

  // Defaults to cookie storage. See Storage for more information
  storage: Storage (optional)

  // See: Server-Side Rendering for more information
  req: Request (optional)

  // See: Global login/logout for more information
  globalLogin: Boolean (True)
  globalLogout: Boolean (True)
}

Returned values

{
  // The session tokens
  token: String | void
  accessToken: String | void
  idToken: String | void
  refreshToken: String | void

  // Manually update the session and storage
  setSession: Function (options) => void

  // Manually remove the session (will clear storage)
  removeSession: Function () => void

  profile: Object | void

  expiration: Date | null

  // If an accessToken or token exists and has not expired
  isAuthenticated: Boolean

  // See: Typescript section for more information
  isAuthenticatedGuard: () => Boolean

  // You can manually invoke the refreshFn
  refreshFn: Function (options) => options

  // If null, the refreshFn is paused.
  refreshInterval: Number | null
}

Features

Global login/logout

By default, browser tabs are kept in sync. If removeSession is called in one tab, it will be called in all tabs

If you wish to disable this behaviour set globalLogout and globalLogin to false in the options

Server Side Rendering

If the req option is used, the package assumes you are performing Server Side Rendering. If you are using the default cookie storage, it will switch to using the request headers and an in-memory store.

If you are using a custom storage, the request will be passed to your store.

Typescript

react-session-hook was written in Typescript and includes typings out of the box.

It also includes the isAuthenticatedGuard function, which acts as a typeguard between an authenticated and authenticated session

import useSession from 'react-session-hook';

interface Profile {
  name: string
}

export default () => {
  // use the JWT values in storage
  const session = useSession<Profile>();

  if (session.isAuthenticatedGuard()) {
    // Typed as session.profile = Profile
    return <div>Logged in as: {session.profile.name}</div>
  } else {
    // Typed as session.profile = null
    return <div>You are logged out</div>
  }
}

Storage

By defaut, your session tokens will be stored as seperate cookies. You can overwrite this by providing custom storage functions in the useSession options

See the cookies storage functions for more information

Misc

The setSesson function can also be used to update some of the options. You can update the refreshFn with setSession({ refreshFn }) or disable the refresh with setSession({ refreshInterval: null })