从零搭建基于 AI 问答平台(2) 安装 Clerk

注册 Clerk

Clerk 是一个以开发者为中心的身份验证和用户管理系统,提供了预构建的React组件和挂钩,用于登录、注册、用户个人资料以及组织管理。 这个模板专为Clerk和Next.js(App Router)设计,让你能够在几分钟内快速启动并运行一个功能齐全的认证流程应用,按下面步骤操作:

  1. 注册 Clerk 账号
  2. 点击 Create application 创建一个应用,也就是我们现在开发的这个项目。
  3. 配置鉴权选项,例如第三方登录提供商。这里需要事先声明的是,第三方登录提供商在开发环境是可以随意增加的,但是在生产环境每一个都需要额外的配置,一般选常用的就可以了。
  4. 进入 Clerk 控制台 找到 API keys,分别是一个公钥和私钥,将这个配置复制到我们项目中的环境变量里。

安装 Clerk

命令行:

1
bun add @clerk/nextjs

修改.env, 添加环境变量:

1
2
3
4
5
6
7
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=xxxx
CLERK_SECRET_KEY=sk_test_xxxx

NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_SIGN_IN_FALLBACK_REDIRECT_URL=/
NEXT_PUBLIC_CLERK_SIGN_UP_FALLBACK_REDIRECT_URL=/

部署登陆页面

登陆页面 src/app/sign-in/[[...sign-in]]/page.tsx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Image from "next/image";
import { Loader2 } from "lucide-react";
import { SignIn, ClerkLoaded,ClerkLoading } from "@clerk/nextjs";

export default function Page() {
return(
<div className="min-h-screen grid grid-cols-1 lg:grid-cols-2">
<div className="h-full lg:flex flex-col items-center justify-center px-4">
<div className="text-center space-y-4 pt-16">
<h1 className="font-bold text-3xl text-[#2E2A47]">
Welcome Back!
</h1>
<p className="text-base text-[#7E8CA0]">
Log in or Create account to get back to your dashboad!
</p>
</div>
<div className="flex items-center justify-center mt-8">
<ClerkLoaded>
<SignIn path="/sign-in" />
</ClerkLoaded>
<ClerkLoading>
<Loader2 className="animate-spin text-muted-foreground" />
</ClerkLoading>
</div>
</div>
<div className="h-full bg-blue-600 hidden lg:flex items-center justify-center">
<Image src="/vim-logo.svg" height={100} width={100} alt="Logo" />
</div>
</div>
)
}

注册页面 src/app/sign-up/[[...sign-up]]/page.tsx:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Image from "next/image";
import { Loader2 } from "lucide-react";
import { SignUp, ClerkLoaded,ClerkLoading } from "@clerk/nextjs";

export default function Page() {
return(
<div className="min-h-screen grid grid-cols-1 lg:grid-cols-2">
<div className="h-full lg:flex flex-col items-center justify-center px-4">
<div className="text-center space-y-4 pt-16">
<h1 className="font-bold text-3xl text-[#2E2A47]">
Welcome Back!
</h1>
<p className="text-base text-[#7E8CA0]">
Log in or Create account to get back to your dashboad!
</p>
</div>
<div className="flex items-center justify-center mt-8">
<ClerkLoaded>
<SignUp path="/sign-up" />
</ClerkLoaded>
<ClerkLoading>
<Loader2 className="animate-spin text-muted-foreground" />
</ClerkLoading>
</div>
</div>
<div className="h-full bg-blue-600 hidden lg:flex items-center justify-center">
<Image src="/vim-logo.svg" height={100} width={100} alt="Logo" />
</div>
</div>
)
}

部署midlleware

添加 src/middleware.ts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'

const isProtectedRoute = createRouteMatcher(['/'])

export default clerkMiddleware(async (auth, req) => {
if (isProtectedRoute(req)) await 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)(.*)',
],
}

作者:Bearalise
出处:从零搭建基于 AI 问答平台(2) 安装 Clerk
版权:本文版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明,必须在文章中给出原文链接。

请我喝杯咖啡吧~

支付宝
微信