Skip to content

Commit f6a00e6

Browse files
committed
doc comment updates and fixes
1 parent 7851f63 commit f6a00e6

File tree

1 file changed

+99
-5
lines changed

1 file changed

+99
-5
lines changed

src/session.ts

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { sealData, unsealData } from 'iron-session';
1414
import { createRemoteJWKSet, decodeJwt, jwtVerify } from 'jose';
1515
import { getConfig } from './config.js';
1616
import { configureSessionStorage, getSessionStorage } from './sessionStorage.js';
17+
import { isResponse } from './utils.js';
18+
import { isRedirect } from '../dist/cjs/utils.js';
1719

1820
// must be a type since this is a subtype of response
1921
// interfaces must conform to the types they extend
@@ -98,22 +100,114 @@ type AuthLoader<Data> = (
98100

99101
type AuthorizedAuthLoader<Data> = (args: LoaderFunctionArgs & { auth: AuthorizedData }) => LoaderReturnValue<Data>;
100102

103+
/**
104+
* This loader handles authentication state, session management, and access token refreshing
105+
* automatically, making it easier to build authenticated routes.
106+
*
107+
* Creates an authentication-aware loader function for React Router.
108+
*
109+
* This loader handles authentication state, session management, and access token refreshing
110+
* automatically, making it easier to build authenticated routes.
111+
*
112+
* @overload
113+
* Basic usage with enforced authentication that redirects unauthenticated users to sign in.
114+
*
115+
* @param loaderArgs - The loader arguments provided by React Router
116+
* @param options - Configuration options with enforced sign-in
117+
*
118+
* @example
119+
* export async function loader({ request }: LoaderFunctionArgs) {
120+
* return authkitLoader(
121+
* { request },
122+
* { ensureSignedIn: true }
123+
* );
124+
* }
125+
*/
101126
async function authkitLoader(
102127
loaderArgs: LoaderFunctionArgs,
103128
options: AuthKitLoaderOptions & { ensureSignedIn: true },
104-
): Promise<DataWithResponseInit<AuthorizedData> | Response>;
105-
129+
): Promise<DataWithResponseInit<AuthorizedData>>;
130+
131+
/**
132+
* This loader handles authentication state, session management, and access token refreshing
133+
* automatically, making it easier to build authenticated routes.
134+
*
135+
* @overload
136+
* Basic usage without enforced authentication, allowing both signed-in and anonymous users.
137+
*
138+
* @param loaderArgs - The loader arguments provided by React Router
139+
* @param options - Optional configuration options
140+
*
141+
* @example
142+
* export async function loader({ request }: LoaderFunctionArgs) {
143+
* return authkitLoader({ request });
144+
* }
145+
*/
106146
async function authkitLoader(
107147
loaderArgs: LoaderFunctionArgs,
108148
options?: AuthKitLoaderOptions,
109149
): Promise<DataWithResponseInit<AuthorizedData | UnauthorizedData>>;
110150

151+
/**
152+
* This loader handles authentication state, session management, and access token refreshing
153+
* automatically, making it easier to build authenticated routes.
154+
*
155+
* @overload
156+
* Custom loader with enforced authentication, providing your own loader function
157+
* that will only be called for authenticated users.
158+
*
159+
* @param loaderArgs - The loader arguments provided by React Router
160+
* @param loader - A custom loader function that receives authentication data
161+
* @param options - Configuration options with enforced sign-in
162+
*
163+
* @example
164+
* export async function loader({ request }: LoaderFunctionArgs) {
165+
* return authkitLoader(
166+
* { request },
167+
* async ({ auth }) => {
168+
* // This will only be called for authenticated users
169+
* const userData = await fetchUserData(auth.accessToken);
170+
* return { userData };
171+
* },
172+
* { ensureSignedIn: true }
173+
* );
174+
* }
175+
*/
111176
async function authkitLoader<Data = unknown>(
112177
loaderArgs: LoaderFunctionArgs,
113178
loader: AuthorizedAuthLoader<Data>,
114179
options: AuthKitLoaderOptions & { ensureSignedIn: true },
115180
): Promise<DataWithResponseInit<Data & AuthorizedData>>;
116181

182+
/**
183+
* This loader handles authentication state, session management, and access token refreshing
184+
* automatically, making it easier to build authenticated routes.
185+
*
186+
* @overload
187+
* Custom loader without enforced authentication, providing your own loader function
188+
* that will be called for both authenticated and unauthenticated users.
189+
*
190+
* @param loaderArgs - The loader arguments provided by React Router
191+
* @param loader - A custom loader function that receives authentication data
192+
* @param options - Optional configuration options
193+
*
194+
* @example
195+
* export async function loader({ request }: LoaderFunctionArgs) {
196+
* return authkitLoader(
197+
* { request },
198+
* async ({ auth }) => {
199+
* if (auth.user) {
200+
* // User is authenticated
201+
* const userData = await fetchUserData(auth.accessToken);
202+
* return { userData };
203+
* } else {
204+
* // User is not authenticated
205+
* return { publicData: await fetchPublicData() };
206+
* }
207+
* }
208+
* );
209+
* }
210+
*/
117211
async function authkitLoader<Data = unknown>(
118212
loaderArgs: LoaderFunctionArgs,
119213
loader: AuthLoader<Data>,
@@ -209,10 +303,10 @@ async function handleAuthLoader(
209303
// auth data plus session cookie header
210304
const loaderResult = await loader({ ...args, auth: auth as AuthorizedData });
211305

212-
if (loaderResult instanceof Response) {
306+
if (isResponse(loaderResult)) {
213307
// If the result is a redirect, return it unedited
214-
if (loaderResult.status >= 300 && loaderResult.status < 400) {
215-
return loaderResult;
308+
if (isRedirect(loaderResult)) {
309+
throw loaderResult;
216310
}
217311

218312
const newResponse = new Response(loaderResult.body, loaderResult);

0 commit comments

Comments
 (0)