Skip to content

Commit 8319f76

Browse files
committed
docs(lib): update documentation for onRefresh method and createAuth function
- Improved documentation for both the `onRefresh` method and the `createAuth` function to clarify their usage and parameters.
1 parent 5cbcd83 commit 8319f76

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ const authClient: AuthClient<AuthTokens, AuthCredentials> = {
7474
},
7575

7676
// Optional: Called when refresh is requested
77-
onRefresh: async (minValidity?: number): Promise<AuthTokens> => {
77+
// The current tokens are passed as the first argument
78+
onRefresh: async (currentTokens: AuthTokens, minValidity?: number): Promise<AuthTokens> => {
7879
// Implement the logic required to refresh the current user tokens
7980
return {
8081
authToken: '...',
@@ -111,13 +112,14 @@ The `AuthClient` instance can be used directly with the `createAuth` function:
111112
```ts
112113
import { createAuth } from '@forward-software/react-auth';
113114

114-
export const { AuthProvider, useAuthClient } = createAuth(authClient);
115+
export const { AuthProvider, useAuthClient, authClient: enhancedAuthClient } = createAuth(authClient);
115116
```
116117

117118
The `createAuth` function returns:
118119

119120
- `AuthProvider`, the context Provider component that should wrap your app and provide access to your AuthClient
120121
- `useAuthClient`, the hook to retrieve and interact with your AuthClient
122+
- `authClient`, the enhanced authentication client instance
121123

122124
#### AuthProvider
123125

lib/src/auth.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ export interface AuthClient<T = AuthTokens, C = AuthCredentials> {
9595
onPreRefresh?(): Promise<void>;
9696

9797
/**
98-
* Optional token refresh handler
99-
* @param {number} [minValidity] - Minimum token validity period in seconds
100-
* @returns {Promise<T>} - Returns refreshed authentication tokens
98+
* Optional token refresh handler.
99+
* Implement this method to handle token refresh logic.
100+
* @param {T} currentTokens - The current authentication tokens.
101+
* @param {number} [minValidity] - Optional minimum token validity period in seconds.
102+
* @returns {Promise<T>} - A promise that resolves with the refreshed authentication tokens.
101103
*/
102104
onRefresh?(currentTokens: T, minValidity?: number): Promise<T>;
103105

@@ -469,11 +471,16 @@ export type AuthProviderProps = PropsWithChildren<{
469471
}>;
470472

471473
/**
472-
* Creates an authentication context and provider for a React application
473-
* @template AC - The AuthClient implementation type
474-
* @template E - The error type used throughout the authentication flow
475-
* @param authClient - The base authentication client to use
476-
* @returns An object containing the AuthProvider component and useAuthClient hook
474+
* Creates an authentication context and provider for a React application.
475+
* It wraps the provided `authClient` with enhanced state management and event handling.
476+
*
477+
* @template AC - The type of the base `AuthClient` implementation.
478+
* @template E - The type of error expected during authentication flows. Defaults to `Error`.
479+
* @param {AC} authClient - The base authentication client instance to use.
480+
* @returns An object containing:
481+
* - `AuthProvider`: A React component to wrap the application or parts of it.
482+
* - `authClient`: The enhanced authentication client instance.
483+
* - `useAuthClient`: A hook to access the enhanced `authClient` within the `AuthProvider`.
477484
*/
478485
export function createAuth<AC extends AuthClient, E extends Error = Error>(authClient: AC) {
479486
// Create a React context containing an AuthClient instance.

0 commit comments

Comments
 (0)