Skip to content

Commit 15b5f00

Browse files
committed
feat: throw error if context accessed outside of provider
1 parent dba009e commit 15b5f00

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules
33
.vscode
44
dist
55
.idea
6+
.vscode
67
log.txt
78
/src/react-app-env.d.ts
89
.env

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Long version;
2828
## Example
2929

3030
```tsx
31-
import { AuthContext, AuthProvider, TAuthConfig, TRefreshTokenExpiredEvent } from "react-oauth2-code-pkce"
31+
import { useAuthContext, AuthProvider, TAuthConfig, TRefreshTokenExpiredEvent } from "react-oauth2-code-pkce"
3232

3333
const authConfig: TAuthConfig = {
3434
clientId: 'myClientID',
@@ -40,7 +40,7 @@ const authConfig: TAuthConfig = {
4040
}
4141

4242
const UserInfo = (): JSX.Element => {
43-
const {token, tokenData} = useContext<IAuthContext>(AuthContext)
43+
const {token, tokenData} = useAuthContext()
4444

4545
return <>
4646
<h4>Access Token</h4>
@@ -71,7 +71,7 @@ npm install react-oauth2-code-pkce
7171

7272
### IAuthContext values
7373

74-
The object that's returned by `useContext(AuthContext)` provides these values;
74+
The object that's returned by `useAuthContext()` provides these values;
7575

7676
```typescript
7777
interface IAuthContext {
@@ -200,9 +200,8 @@ This can be solved by marking the module with `use client` and importing the com
200200

201201
```tsx
202202
'use client'
203-
import {useContext} from "react";
204203
import dynamic from 'next/dynamic'
205-
import {TAuthConfig,TRefreshTokenExpiredEvent, AuthContext} from 'react-oauth2-code-pkce'
204+
import {TAuthConfig, TRefreshTokenExpiredEvent, useAuthContext} from 'react-oauth2-code-pkce'
206205

207206
const AuthProvider = dynamic(
208207
()=> import("react-oauth2-code-pkce")

src/AuthContext.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import type {
1515
TTokenResponse,
1616
} from './types'
1717

18+
export const DEFAULT_CONTEXT_TOKEN = 'DEFAULT_CONTEXT_TOKEN'
19+
20+
// TODO: Change to undefined context and update useAuthContext accordingly in v2
1821
export const AuthContext = createContext<IAuthContext>({
19-
token: '',
22+
token: DEFAULT_CONTEXT_TOKEN,
2023
login: () => null,
2124
logIn: () => null,
2225
logOut: () => null,

src/index.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// If you want to run the project locally you will need to update the authConfig object with your own auth provider
77
// ##########################################
88

9-
import React, { useContext } from 'react'
9+
import React from 'react'
1010
import { createRoot } from 'react-dom/client'
11-
import { AuthContext, AuthProvider } from './AuthContext'
11+
import { AuthProvider } from './AuthContext'
12+
import { useAuthContext } from './useAuthContext'
1213

1314
// Get auth provider info from "https://keycloak.ofstad.xyz/realms/master/.well-known/openid-configuration"
1415
/** @type {import('./types').TAuthConfig} */
@@ -31,7 +32,7 @@ const authConfig = {
3132
}
3233

3334
function LoginInfo() {
34-
const { tokenData, token, idTokenData, logIn, logOut, error, loginInProgress } = useContext(AuthContext)
35+
const { tokenData, token, idTokenData, logIn, logOut, error, loginInProgress } = useAuthContext()
3536

3637
if (loginInProgress) return null
3738
return (

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export type {
55
TAuthConfig,
66
TRefreshTokenExpiredEvent,
77
} from './types'
8+
export { useAuthContext } from './useAuthContext'

src/useAuthContext.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useContext } from 'react'
2+
import { AuthContext, DEFAULT_CONTEXT_TOKEN } from './AuthContext'
3+
import type { IAuthContext } from './types'
4+
5+
export function useAuthContext(): IAuthContext {
6+
const ctx = useContext(AuthContext)
7+
if (ctx.token === DEFAULT_CONTEXT_TOKEN) {
8+
throw new Error('useAuthContext must be used within an AuthProvider')
9+
}
10+
return ctx
11+
}

0 commit comments

Comments
 (0)