-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Nextjs 13 App Directory Utility Methods (#4103)
* working * fix toolkit app * fix add directive * add back secure password * changeset * improve blitz rpc structure * make it work * fix most integration tests * fix the tests * fix import error * fix unit test * fix tests * remove breaking changes * remove another breaking change * fix typescript declaration * fix errorboundary redirect issues * cleanup * upgrade to next13 * update pnpm-lock * add Server Plugin Export * fix lint error * fix build errors * fixes * Solve NextRouter is not mounted * fix * fix issues * pnpm lock fix * rename session function and add auth utility * remove potential implicit warning * fix deps and cleanup * fix secure-password missing file * fix possibility of crsf token cookie being null * remove secure-password breaking changes * Update cuddly-singers-perform.md * rename functions and remove headers * Revert "remove secure-password breaking changes" This reverts commit abccf93. * unsupported auth methods in react server components * remove authorizedContext: To be done in another PR * add integration test for app dir * fix duplicate package name * Revert "remove authorizedContext: To be done in another PR" This reverts commit 46fe8af. * pnpm lock * integration test * remove empty then and add void operator * add new invokeResolver function * fix bug in useAuthenticatedBlitzContext * Create .changeset/tall-radios-clean.md * overload the invoke function for the new RSC usecase * invoke works without implicit passing of arguments * fix pnpm lock
- Loading branch information
1 parent
cadefb8
commit 37aeaa7
Showing
10 changed files
with
179 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
"next-blitz-auth": patch | ||
"@blitzjs/auth": patch | ||
"@blitzjs/rpc": patch | ||
"blitz": patch | ||
--- | ||
|
||
feature: Nextjs 13 App Directory Utility Methods | ||
|
||
### 🔧 New Blitz Auth Hook `useAuthenticatedBlitzContext` | ||
|
||
This hook is implemented as the replacement of the [`BlitzPage` seurity auth utilities](https://blitzjs.com/docs/authorization#secure-your-pages) provided for the pages directory to work with React Server Components in the Nextjs 13 app directory | ||
It can be used in any asynchronous server component be it in `page.ts` or in the layouts in `layout.ts` | ||
It uses the new [`redirect` function](https://beta.nextjs.org/docs/api-reference/redirect) to provide the required authorization in server side | ||
|
||
#### API | ||
```ts | ||
useAuthenticatedBlitzContext({ | ||
redirectTo, | ||
redirectAuthenticatedTo, | ||
role, | ||
}: { | ||
redirectTo?: string | RouteUrlObject | ||
redirectAuthenticatedTo?: string | RouteUrlObject | ((ctx: Ctx) => string | RouteUrlObject) | ||
role?: string | string[] | ||
}): Promise<void> | ||
``` | ||
|
||
#### Usage | ||
**Example Usage in React Server Component in `app` directory in Next 13** | ||
```ts | ||
import {getAppSession, useAuthenticatedBlitzContext} from "src/blitz-server" | ||
... | ||
await useAuthenticatedBlitzContext({ | ||
redirectTo: "/auth/login", | ||
role: ["admin"], | ||
redirectAuthenticatedTo: "/dashboard", | ||
}) | ||
``` | ||
|
||
### 🔧 New Blitz RPC Hook `invokeResolver` | ||
|
||
#### API | ||
```ts | ||
invokeResolver<T extends (...args: any) => any, TInput = FirstParam<T>>( | ||
queryFn: T, | ||
params: TInput, | ||
): Promise<PromiseReturnType<T>> | ||
``` | ||
|
||
#### Example Usage | ||
|
||
```ts | ||
... | ||
import {invokeResolver, useAuthenticatedBlitzContext} from "../src/blitz-server" | ||
import getCurrentUser from "../src/users/queries/getCurrentUser" | ||
|
||
export default async function Home() { | ||
await useAuthenticatedBlitzContext({ | ||
redirectTo: "/auth/login", | ||
}) | ||
const user = await invokeResolver(getCurrentUser, null) | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {useAuthenticatedBlitzContext} from "@blitzjs/auth" | ||
|
||
export default async function RootLayout({children}: {children: React.ReactNode}) { | ||
await useAuthenticatedBlitzContext({ | ||
redirectAuthenticatedTo: "/", | ||
}) | ||
return <>{children}</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createServerPlugin} from "blitz" | ||
import {invoke} from "../client/invoke" | ||
|
||
export const RpcServerPlugin = createServerPlugin(() => { | ||
return { | ||
requestMiddlewares: [], | ||
exports: () => ({ | ||
invoke, | ||
}), | ||
} | ||
}) |