-
-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Blitz RPC endpoint to the app directory (#4341)
* feat: make `rpchandler` work with `Request` and return a `Response` * rename: rpcRequestHandler * feat: improvements * blitz-auth now works * return headers * working * cleanup * working sveltekit, with regressin of next.js app dir * cleanup * pnpm lock fix * Update packages/blitz-auth/src/server/auth-plugin.ts * fix build * more work * fixes * fix issues with auth * maybe required breaking change * pointless test * Update packages/blitz-auth/package.json * fixes * fix * get all tests passing * more fixes * changeset * fixes * fix * pnpm lock update * fix * update pnpm lcok * revert unnecessary changes * imporve api naming * cleanup * fix * Update integration-tests/next-13-app-dir/src/blitz-server.ts * Apply suggestions from code review * remove unrelated changes * Update packages/blitz-auth/src/server/auth-sessions.ts * fix types * fix overload * remove dependence on http module * review changes * oops * fix * fix types * Revert "fix types" This reverts commit b06a4fb. * Revert "fix" This reverts commit 47d0cdd. * Revert "oops" This reverts commit 94cb558. * Revert "review changes" This reverts commit 14d8eb2. * fix the logic * sort deps * template fixes * chore: add changeset * chore: remove outdated changeset * fix changeset formatting * Update .changeset/tidy-gorillas-confess.md * remove `blitzAuthRpcMiddleware` * remove uses of any * fix jsdoc * no var * separate the type imports * fix unsupported method of session in rsc * fix * api changes * Apply suggestions from code review Co-authored-by: Brandon Bayer <[email protected]> * Apply suggestions from code review * Update .changeset/tidy-gorillas-confess.md * fix * fic --------- Co-authored-by: Brandon Bayer <[email protected]>
- Loading branch information
1 parent
2560175
commit 3b10b13
Showing
62 changed files
with
1,329 additions
and
700 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,47 @@ | ||
--- | ||
"blitz": minor | ||
"@blitzjs/auth": minor | ||
"@blitzjs/next": minor | ||
"@blitzjs/rpc": minor | ||
"@blitzjs/generator": minor | ||
--- | ||
|
||
feat: add blitz auth support for the Web `Request` API standard | ||
|
||
Usage using the new `withBlitzAuth` adapter in the App Router: | ||
|
||
```ts | ||
import {withBlitzAuth} from "app/blitz-server" | ||
|
||
export const {POST} = withBlitzAuth({ | ||
POST: async (_request, _params, ctx) => { | ||
const session = ctx.session | ||
await session.$revoke() | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
userId: session.userId, | ||
}), | ||
{status: 200}, | ||
) | ||
}, | ||
}) | ||
``` | ||
|
||
feat: New Blitz RPC handler meant to with the next.js app router `route.ts` files | ||
|
||
Usage using the new `rpcAppHandler` function | ||
|
||
```ts | ||
// app/api/rpc/[[...blitz]]/route.ts | ||
import {rpcAppHandler} from "@blitzjs/rpc" | ||
import {withBlitzAuth} from "app/blitz-server" | ||
|
||
// Usage with blitz auth | ||
export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler()) | ||
|
||
// Standalone usage | ||
export const {GET, POST, HEAD} = rpcAppHandler() | ||
``` | ||
|
||
chore: Update the app directory starter |
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
Binary file not shown.
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,4 @@ | ||
import {rpcAppHandler} from "@blitzjs/rpc" | ||
import {withBlitzAuth} from "src/blitz-server" | ||
|
||
export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler()) |
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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,15 @@ | ||
import {withBlitzAuth} from "../../../src/blitz-server" | ||
|
||
export const {POST} = withBlitzAuth({ | ||
POST: async (_request, _params, ctx) => { | ||
const session = ctx.session | ||
await session.$revoke() | ||
|
||
return new Response( | ||
JSON.stringify({ | ||
userId: session.userId, | ||
}), | ||
{status: 200}, | ||
) | ||
}, | ||
}) |
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 {H} from "@blitzjs/auth/dist/index-0ecbee46" | ||
import {withBlitzAuth} from "../../../src/blitz-server" | ||
|
||
const emptyResponse = async () => { | ||
return new Response(null, {status: 200}) | ||
} | ||
|
||
export const {POST, HEAD} = withBlitzAuth({ | ||
POST: emptyResponse, | ||
HEAD: emptyResponse, | ||
}) |
4 changes: 4 additions & 0 deletions
4
integration-tests/next-13-app-dir/app/api/rpc/[[...blitz]]/route.ts
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,4 @@ | ||
import {rpcAppHandler} from "@blitzjs/rpc" | ||
import {withBlitzAuth} from "../../../../src/blitz-server" | ||
|
||
export const {GET, POST, HEAD} = withBlitzAuth(rpcAppHandler()) |
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,38 @@ | ||
import {withBlitzAuth} from "../../../src/blitz-server" | ||
import prisma from "../../../db/index" | ||
import {Role} from "../../../types" | ||
|
||
export const authenticateUser = async (email: string, password: string) => { | ||
const user = await prisma.user.findFirst({where: {email}}) | ||
|
||
if (!user) throw new Error("Authentication Error") | ||
await prisma.user.update({where: {id: user.id}, data: {hashedPassword: password}}) | ||
|
||
const {hashedPassword, ...rest} = user | ||
return rest | ||
} | ||
|
||
export const {POST} = withBlitzAuth({ | ||
POST: async (request: Request, context, ctx) => { | ||
const {searchParams} = new URL(request.url) | ||
const user = await authenticateUser( | ||
searchParams.get("email") as string, | ||
searchParams.get("password") as string, | ||
) | ||
|
||
await ctx.session.$create({ | ||
userId: user.id, | ||
role: user.role as Role, | ||
}) | ||
|
||
return new Response( | ||
JSON.stringify({email: searchParams.get("email"), userId: ctx.session.userId}), | ||
{ | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}, | ||
) | ||
}, | ||
}) |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
integration-tests/next-13-app-dir/pages/api/rpc/[[...blitz]].ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.