👤
react-session-hook
Stateful sessions made easy
Overview
✔️ Simple API
✔️ Supports JWTs, string tokens and custom profiles
✔️ Supports handling multiple tokens (access-token, id-token, refresh-token)
✔️ Automatic logout and periodic refresh functions
✔️ Customisable persistent storage
✔️ Global login and logout events across tabs
✔️ Typescript typings and helper functions
Coming soon
📋Server-side rendering
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
{
initialAccessToken: String (optional)
initialIdToken: String (optional)
initialRefreshToken String (optional)
initialToken: String (optional)
initialProfile: Object (optional)
jwt: Boolean (True)
expiration: Date (optional)
refreshFn: async (session) => Session (optional)
refreshInterval: Number (1 * 60 * 60 * 1000)
profileFn: (String) => Object | void
storage: Storage (optional)
req: Request (optional)
globalLogin: Boolean (True)
globalLogout: Boolean (True)
}
Returned values
{
token: String | void
accessToken: String | void
idToken: String | void
refreshToken: String | void
setSession: Function (options) => void
removeSession: Function () => void
profile: Object | void
expiration: Date | null
isAuthenticated: Boolean
isAuthenticatedGuard: () => Boolean
refreshFn: Function (options) => options
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
📋This feature is coming soon
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 () => {
const session = useSession<Profile>();
if (session.isAuthenticatedGuard()) {
return <div>Logged in as: {session.profile.name}</div>
} else {
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 })