@@ -182,30 +182,36 @@ export async function getSession(
182
182
}
183
183
184
184
export async function getBlitzContext ( ) : Promise < Ctx > {
185
- const { headers, cookies} = await import ( "next/headers" ) . catch ( ( ) => {
186
- throw new Error (
187
- "getBlitzContext() can only be used in React Server Components in Nextjs 13 or higher" ,
185
+ try {
186
+ //using eval to avoid bundling next/headers
187
+ const { headers, cookies} = eval ( "require('next/headers')" )
188
+ const req = new IncomingMessage ( new Socket ( ) ) as IncomingMessage & {
189
+ cookies : { [ key : string ] : string }
190
+ }
191
+ req . headers = Object . fromEntries ( headers ( ) )
192
+ const csrfToken = cookies ( ) . get ( COOKIE_CSRF_TOKEN ( ) )
193
+ if ( csrfToken ) {
194
+ req . headers [ HEADER_CSRF ] = csrfToken . value
195
+ }
196
+ req . cookies = Object . fromEntries (
197
+ cookies ( )
198
+ . getAll ( )
199
+ . map ( ( c : { name : string ; value : string } ) => [ c . name , c . value ] ) ,
188
200
)
189
- } )
190
- const req = new IncomingMessage ( new Socket ( ) ) as IncomingMessage & {
191
- cookies : { [ key : string ] : string }
192
- }
193
- req . headers = Object . fromEntries ( headers ( ) )
194
- const csrfToken = cookies ( ) . get ( COOKIE_CSRF_TOKEN ( ) )
195
- if ( csrfToken ) {
196
- req . headers [ HEADER_CSRF ] = csrfToken . value
197
- }
198
- req . cookies = Object . fromEntries (
199
- cookies ( )
200
- . getAll ( )
201
- . map ( ( c : { name : string ; value : string } ) => [ c . name , c . value ] ) ,
202
- )
203
- const res = new ServerResponse ( req )
204
- const session = await getSession ( req , res , true )
205
- const ctx : Ctx = {
206
- session,
201
+ const res = new ServerResponse ( req )
202
+ const session = await getSession ( req , res , true )
203
+ const ctx : Ctx = {
204
+ session,
205
+ }
206
+ return ctx
207
+ } catch ( e ) {
208
+ if ( ( e as NodeJS . ErrnoException ) . code === "MODULE_NOT_FOUND" ) {
209
+ throw new Error (
210
+ "Usage of `useAuthenticatedBlitzContext` is supported only in next.js 13.0.0 and above. Please upgrade your next.js version." ,
211
+ )
212
+ }
213
+ throw e
207
214
}
208
- return ctx
209
215
}
210
216
211
217
interface RouteUrlObject extends Pick < UrlObject , "pathname" | "query" | "href" > {
@@ -227,58 +233,64 @@ export async function useAuthenticatedBlitzContext({
227
233
} )
228
234
const ctx : Ctx = await getBlitzContext ( )
229
235
const userId = ctx . session . userId
230
- const { redirect} = await import ( "next/navigation" ) . catch ( ( ) => {
231
- throw new Error (
232
- "useAuthenticatedBlitzContext() can only be used in React Server Components in Nextjs 13 or higher" ,
233
- )
234
- } )
235
- if ( userId ) {
236
- debug ( "[useAuthenticatedBlitzContext] User is authenticated" )
237
- if ( redirectAuthenticatedTo ) {
238
- if ( typeof redirectAuthenticatedTo === "function" ) {
239
- redirectAuthenticatedTo = redirectAuthenticatedTo ( ctx )
236
+ try {
237
+ //using eval to avoid bundling next/navigation
238
+ const { redirect} = eval ( "require('next/navigation')" )
239
+ if ( userId ) {
240
+ debug ( "[useAuthenticatedBlitzContext] User is authenticated" )
241
+ if ( redirectAuthenticatedTo ) {
242
+ if ( typeof redirectAuthenticatedTo === "function" ) {
243
+ redirectAuthenticatedTo = redirectAuthenticatedTo ( ctx )
244
+ }
245
+ const redirectUrl =
246
+ typeof redirectAuthenticatedTo === "string"
247
+ ? redirectAuthenticatedTo
248
+ : formatWithValidation ( redirectAuthenticatedTo )
249
+ debug ( "[useAuthenticatedBlitzContext] Redirecting to" , redirectUrl )
250
+ if ( role ) {
251
+ try {
252
+ ctx . session . $authorize ( role )
253
+ } catch ( e ) {
254
+ log . info ( "Authentication Redirect: " + customChalk . dim ( `Role ${ role } ` ) , redirectTo )
255
+ redirect ( redirectUrl )
256
+ }
257
+ } else {
258
+ log . info ( "Authentication Redirect: " + customChalk . dim ( "(Authenticated)" ) , redirectUrl )
259
+ redirect ( redirectUrl )
260
+ }
240
261
}
241
- const redirectUrl =
242
- typeof redirectAuthenticatedTo === "string"
243
- ? redirectAuthenticatedTo
244
- : formatWithValidation ( redirectAuthenticatedTo )
245
- debug ( "[useAuthenticatedBlitzContext] Redirecting to" , redirectUrl )
246
- if ( role ) {
262
+ if ( redirectTo && role ) {
263
+ debug ( "[useAuthenticatedBlitzContext] redirectTo and role are both defined." )
247
264
try {
248
265
ctx . session . $authorize ( role )
249
266
} catch ( e ) {
250
- log . info ( "Authentication Redirect: " + customChalk . dim ( `Role ${ role } ` ) , redirectTo )
251
- redirect ( redirectUrl )
267
+ log . error ( "Authorization Error: " + ( e as Error ) . message )
268
+ if ( typeof redirectTo !== "string" ) {
269
+ redirectTo = formatWithValidation ( redirectTo )
270
+ }
271
+ log . info ( "Authorization Redirect: " + customChalk . dim ( `Role ${ role } ` ) , redirectTo )
272
+ redirect ( redirectTo )
252
273
}
253
- } else {
254
- log . info ( "Authentication Redirect: " + customChalk . dim ( "(Authenticated)" ) , redirectUrl )
255
- redirect ( redirectUrl )
256
274
}
257
- }
258
- if ( redirectTo && role ) {
259
- debug ( "[useAuthenticatedBlitzContext] redirectTo and role are both defined." )
260
- try {
261
- ctx . session . $authorize ( role )
262
- } catch ( e ) {
263
- log . error ( "Authorization Error: " + ( e as Error ) . message )
275
+ } else {
276
+ debug ( "[useAuthenticatedBlitzContext] User is not authenticated" )
277
+ if ( redirectTo ) {
264
278
if ( typeof redirectTo !== "string" ) {
265
279
redirectTo = formatWithValidation ( redirectTo )
266
280
}
267
- log . info ( "Authorization Redirect: " + customChalk . dim ( `Role ${ role } ` ) , redirectTo )
281
+ log . info ( "Authentication Redirect: " + customChalk . dim ( "(Not authenticated)" ) , redirectTo )
268
282
redirect ( redirectTo )
269
283
}
270
284
}
271
- } else {
272
- debug ( "[useAuthenticatedBlitzContext] User is not authenticated" )
273
- if ( redirectTo ) {
274
- if ( typeof redirectTo !== "string" ) {
275
- redirectTo = formatWithValidation ( redirectTo )
276
- }
277
- log . info ( "Authentication Redirect: " + customChalk . dim ( "(Not authenticated)" ) , redirectTo )
278
- redirect ( redirectTo )
285
+ return ctx as AuthenticatedCtx
286
+ } catch ( e ) {
287
+ if ( ( e as NodeJS . ErrnoException ) . code === "MODULE_NOT_FOUND" ) {
288
+ throw new Error (
289
+ "Usage of `useAuthenticatedBlitzContext` is supported only in next.js 13.0.0 and above. Please upgrade your next.js version." ,
290
+ )
279
291
}
292
+ throw e
280
293
}
281
- return ctx as AuthenticatedCtx
282
294
}
283
295
284
296
const makeProxyToPublicData = < T extends SessionContextClass > ( ctxClass : T ) : T => {
0 commit comments