Skip to content

Commit

Permalink
Adds support for changing the browser storage key prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackburn29 authored and soofstad committed Sep 26, 2023
1 parent 615a8af commit f77d5ca
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ type TAuthConfig = {
// NOTE: Many authentication servers will keep the client logged in by cookies. You should therefore use
// the 'logout()'-function to properly logout the client. Or configure your server not to issue cookies.
storage?: 'local' | 'session' // default: 'local'
// Sets the prefix used when storing login state
storageKeyPrefix?: string // default: 'ROCP_'
// Set to false if you need to access the urlParameters sent back from the login server.
clearURL?: boolean // default: true
// Can be used to provide any non-standard parameters to the authentication request
Expand Down
34 changes: 22 additions & 12 deletions src/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,41 @@ export const AuthProvider = ({ authConfig, children }: IAuthProvider) => {
const config: TInternalConfig = useMemo(() => createInternalConfig(authConfig), [authConfig])

const [refreshToken, setRefreshToken] = useBrowserStorage<string | undefined>(
'ROCP_refreshToken',
'refreshToken',
undefined,
config.storage
config.storage,
config.storageKeyPrefix
)
const [refreshTokenExpire, setRefreshTokenExpire] = useBrowserStorage<number>(
'ROCP_refreshTokenExpire',
'refreshTokenExpire',
epochAtSecondsFromNow(2 * FALLBACK_EXPIRE_TIME),
config.storage
config.storage,
config.storageKeyPrefix
)
const [token, setToken] = useBrowserStorage<string>('ROCP_token', '', config.storage)
const [token, setToken] = useBrowserStorage<string>('ROCP_token', '', config.storage, config.storageKeyPrefix)
const [tokenExpire, setTokenExpire] = useBrowserStorage<number>(
'ROCP_tokenExpire',
'tokenExpire',
epochAtSecondsFromNow(FALLBACK_EXPIRE_TIME),
config.storage
config.storage,
config.storageKeyPrefix
)
const [idToken, setIdToken] = useBrowserStorage<string | undefined>(
'idToken',
undefined,
config.storage,
config.storageKeyPrefix
)
const [idToken, setIdToken] = useBrowserStorage<string | undefined>('ROCP_idToken', undefined, config.storage)
const [loginInProgress, setLoginInProgress] = useBrowserStorage<boolean>(
'ROCP_loginInProgress',
'loginInProgress',
false,
config.storage
config.storage,
config.storageKeyPrefix
)
const [refreshInProgress, setRefreshInProgress] = useBrowserStorage<boolean>(
'ROCP_refreshInProgress',
'refreshInProgress',
false,
config.storage
config.storage,
config.storageKeyPrefix
)
const [tokenData, setTokenData] = useState<TTokenData | undefined>()
const [idTokenData, setIdTokenData] = useState<TTokenData | undefined>()
Expand Down
8 changes: 7 additions & 1 deletion src/Hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useEffect, useState } from 'react'

function useBrowserStorage<T>(key: string, initialValue: T, type: 'session' | 'local'): [T, (v: T) => void] {
function useBrowserStorage<T>(
key: string,
initialValue: T,
type: 'session' | 'local',
prefix?: string
): [T, (v: T) => void] {
const storage = type === 'session' ? sessionStorage : localStorage
key = `${prefix ?? ''}${key}`

const [storedValue, setStoredValue] = useState<T>(() => {
const item = storage.getItem(key)
Expand Down
2 changes: 2 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type TAuthConfig = {
tokenExpiresIn?: number
refreshTokenExpiresIn?: number
storage?: 'session' | 'local'
storageKeyPrefix?: 'ROCP_'
}

export type TRefreshTokenExpiredEvent = {
Expand Down Expand Up @@ -103,4 +104,5 @@ export type TInternalConfig = {
tokenExpiresIn?: number
refreshTokenExpiresIn?: number
storage: 'session' | 'local'
storageKeyPrefix?: string
}
2 changes: 2 additions & 0 deletions src/authConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
postLogin = () => null,
onRefreshTokenExpire = undefined,
storage = 'local',
storageKeyPrefix = 'ROCP_',
}: TAuthConfig = passedConfig

const config: TInternalConfig = {
Expand All @@ -28,6 +29,7 @@ export function createInternalConfig(passedConfig: TAuthConfig): TInternalConfig
postLogin: postLogin,
onRefreshTokenExpire: onRefreshTokenExpire,
storage: storage,
storageKeyPrefix: storageKeyPrefix,
}
validateConfig(config)
return config
Expand Down
3 changes: 2 additions & 1 deletion tests/jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ beforeEach(() => {

global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
global.crypto.subtle = nodeCrypto.subtle

global.crypto.subtle = nodeCrypto.webcrypto.subtle;

delete window.location
const location = new URL('https://www.example.com')
Expand Down
2 changes: 1 addition & 1 deletion tests/login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('First page visit should redirect to auth provider for login', async () =>

test('Attempting to login with and unsecure context should raise error', async () => {
// @ts-ignore
global.crypto.subtle.digest = undefined
window.crypto.subtle.digest = undefined
render(
<AuthProvider authConfig={authConfig}>
<AuthConsumer />
Expand Down
1 change: 1 addition & 0 deletions tests/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AuthContext, TAuthConfig } from '../src'
import React, { useContext } from 'react'

export const authConfig: TAuthConfig = {
autoLogin: true,
clientId: 'myClientID',
authorizationEndpoint: 'myAuthEndpoint',
tokenEndpoint: 'myTokenEndpoint',
Expand Down

0 comments on commit f77d5ca

Please sign in to comment.