Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Nextjs 13 App Directory Utility Methods #4103

Merged
merged 63 commits into from
Mar 29, 2023
Merged

Conversation

siddhsuresh
Copy link
Member

@siddhsuresh siddhsuresh commented Mar 18, 2023

Continues #4079

What are the changes and their implications?

🔧 New Blitz Auth Hook useAuthenticatedBlitzContext

This hook is implemented as the replacement of the BlitzPage seurity auth utilities 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 to provide the required authorization in server side

API

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

import {getAppSession, useAuthenticatedBlitzContext} from "src/blitz-server"
...
await useAuthenticatedBlitzContext({
    redirectTo: "/auth/login",
    role: ["admin"],
    redirectAuthenticatedTo: "/dashboard",
})

🔧 New Blitz RPC Hook invokeResolver

API

invokeResolver<T extends (...args: any) => any, TInput = FirstParam<T>>(
  queryFn: T,
  params: TInput,
): Promise<PromiseReturnType<T>> 

Example Usage

...
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)
...

Feature Checklist

@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2023

🦋 Changeset detected

Latest commit: 7ca46ac

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
next-blitz-auth Patch
@blitzjs/auth Patch
@blitzjs/rpc Patch
blitz Patch
@blitzjs/next Patch
@blitzjs/codemod Patch
@blitzjs/config Patch
@blitzjs/generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@siddhsuresh siddhsuresh changed the base branch from main to next13-app March 18, 2023 16:12
@siddhsuresh siddhsuresh changed the title Nextjs 13 App Directory Utility Methods feature: Nextjs 13 App Directory Utility Methods Mar 18, 2023
Base automatically changed from next13-app to main March 24, 2023 05:40
@siddhsuresh siddhsuresh merged commit 37aeaa7 into main Mar 29, 2023
@siddhsuresh siddhsuresh deleted the app-dir-utilities branch March 29, 2023 13:35
@blitzjs-bot blitzjs-bot added this to Done in Toolkit Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants