从零搭建基于 AI GitHub 分析平台 (3) 同步 Clerk 用户数据

修改数据结构

修改 prisma/schema.prisma

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
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

firstName String?
lastName String?
imageUrl String?

emailAddress String @unique

credits Int @default(150)
}

运行命令,更新数据库:

1
bun prisma db push

添加环境变量

1
NEXT_PUBLIC_CLERK_SIGN_UP_FORCE_REDIRECT_URL='/usersync'

添加 usersync 页面

添加 src/app/usersync/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
32
33
34
35
36
37
38
import { db } from "@/server/db";
import { auth, clerkClient } from "@clerk/nextjs/server";
import { notFound, redirect } from "next/navigation";

const UserSync = async ()=> {
const { userId } = await auth();
if (!userId) {
throw new Error('User not found');
}
const client = await clerkClient();
const user = await client.users.getUser(userId);

if (!user.emailAddresses[0]?.emailAddress){
return notFound();
}

await db.user.upsert({
where: {
emailAddress: user.emailAddresses[0]?.emailAddress
},
update: {
firstName: user.firstName,
lastName: user.lastName,
imageUrl: user.imageUrl
},
create: {
id: userId,
emailAddress: user.emailAddresses[0]?.emailAddress,
firstName: user.firstName,
lastName: user.lastName,
imageUrl:user.imageUrl
},
});

return redirect("/dashboard");
}

export default UserSync;

测试

测试前,可以看user表里是没有数据的:

开始登陆:

登陆完成后,可以看到表记录增加了:


作者:Bearalise
出处:从零搭建基于 AI GitHub 分析平台 (3) 同步 Clerk 用户数据
版权:本文版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明,必须在文章中给出原文链接。
title: 从零搭建基于 AI GitHub 分析平台 (3) 同步 Clerk 用户数据
date: 2025-03-20 08:46:15
categories:

  • 从零搭建
    tags:
  • NextJS
  • Clerk

修改数据结构

修改 prisma/schema.prisma

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
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

firstName String?
lastName String?
imageUrl String?

emailAddress String @unique

credits Int @default(150)
}

运行命令,更新数据库:

1
bun prisma db push

添加环境变量

1
NEXT_PUBLIC_CLERK_SIGN_UP_FORCE_REDIRECT_URL='/usersync'

添加 usersync 页面

添加 src/app/usersync/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
32
33
34
35
36
37
38
import { db } from "@/server/db";
import { auth, clerkClient } from "@clerk/nextjs/server";
import { notFound, redirect } from "next/navigation";

const UserSync = async ()=> {
const { userId } = await auth();
if (!userId) {
throw new Error('User not found');
}
const client = await clerkClient();
const user = await client.users.getUser(userId);

if (!user.emailAddresses[0]?.emailAddress){
return notFound();
}

await db.user.upsert({
where: {
emailAddress: user.emailAddresses[0]?.emailAddress
},
update: {
firstName: user.firstName,
lastName: user.lastName,
imageUrl: user.imageUrl
},
create: {
id: userId,
emailAddress: user.emailAddresses[0]?.emailAddress,
firstName: user.firstName,
lastName: user.lastName,
imageUrl:user.imageUrl
},
});

return redirect("/dashboard");
}

export default UserSync;

测试

测试前,可以看user表里是没有数据的:

开始登陆:

登陆完成后,可以看到表记录增加了:


作者:Bearalise
出处:从零搭建基于 AI GitHub 分析平台 (3) 同步 Clerk 用户数据
版权:本文版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明,必须在文章中给出原文链接。

请我喝杯咖啡吧~

支付宝
微信