为什么在 Next.js 应用程序上使用 Clerk 时“const { userId } = wait auth()”返回 null?

问题描述 投票:0回答:1

我遇到了一个问题,即 Clerk 的 auth() 帮助程序返回的 userId 为空,即使我使用 ClerkProvider 包装我的应用程序。以下是我的设置和代码的详细信息:

文档:https://clerk.com/docs/references/nextjs/auth#use-auth-to-retrieve-user-id

export async function CreateProduct(unsafeData: z.infer<typeof productDetailsSchema>) {
    //this returns null
    const { userId }: { userId: string | null } = await auth()
    const { success, data } =  productDetailsSchema.safeParse(unsafeData)

    if (userId === null) {
        return { 
            error: true, 
            message: 'There was an error creating your product. userId is null' 
        }
    } else if (!success) {
        return { 
            error: true, 
            message: 'There was an error creating your product. Request was unsuccesful' 
        }
    }

    const { id } = await createProductDb({ ...data, clerkUserId: userId })

    redirect(`/dashboard/products/${id}/edit?tab=countries`)
}

这是我的中间件设置:

import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";

const isPublicRoute = createRouteMatcher([
  '/', 
  '/sign-in(.*)',
  '/sign-up(.*)',
  '/api(.*)'
]) 

export default clerkMiddleware((auth, req) => {
  if (!isPublicRoute(req)) {
    auth.protect();
  }
})

export const config = {
  matcher: [
    // Skip Next.js internals and all static files, unless found in search params
    '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
    // Always run for API routes
    '/(api|trpc)(.*)',
  ],
};

任何帮助将不胜感激!

预期行为: auth() 帮助器应该返回经过身份验证的用户的 userId。

实际行为: userId 始终为空。

Clerk 和下一个版本: @clerk/[电子邮件受保护] @clerk/[电子邮件受保护] [电子邮件受保护]

authentication next.js null userid clerk
1个回答
0
投票

我在新部署中面临同样的问题。你找到解决办法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.