Skip to content

Commit dd109a4

Browse files
committed
feat(auth): add Google OAuth support
1 parent 3ad3030 commit dd109a4

File tree

13 files changed

+162
-36
lines changed

13 files changed

+162
-36
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
AUTH_GITHUB_ID = ""
22
AUTH_GITHUB_SECRET = ""
3+
AUTH_GOOGLE_ID = ""
4+
AUTH_GOOGLE_SECRET = ""
35
AUTH_SECRET = ""
46

57
CLOUDFLARE_API_TOKEN = ""

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<a href="#OpenAPI">OpenAPI</a> •
2323
<a href="#环境变量">环境变量</a> •
2424
<a href="#Github OAuth App 配置">Github OAuth App 配置</a> •
25+
<a href="#Google OAuth App 配置">Google OAuth App 配置</a> •
2526
<a href="#贡献">贡献</a> •
2627
<a href="#许可证">许可证</a> •
2728
<a href="#交流群">交流群</a> •
@@ -782,6 +783,8 @@ console.log('分享链接:', `https://your-domain.com/shared/message/${data.toke
782783
### 认证相关
783784
- `AUTH_GITHUB_ID`: GitHub OAuth App ID
784785
- `AUTH_GITHUB_SECRET`: GitHub OAuth App Secret
786+
- `AUTH_GOOGLE_ID`: Google OAuth App ID
787+
- `AUTH_GOOGLE_SECRET`: Google OAuth App Secret
785788
- `AUTH_SECRET`: NextAuth Secret,用来加密 session,请设置一个随机字符串
786789

787790
### Cloudflare 配置
@@ -796,16 +799,29 @@ console.log('分享链接:', `https://your-domain.com/shared/message/${data.toke
796799

797800
## Github OAuth App 配置
798801

799-
- 登录 [Github Developer](https://github.com/settings/developers) 创建一个新的 OAuth App
800-
- 生成一个新的 `Client ID``Client Secret`
801-
- 设置 `Application name``<your-app-name>`
802-
- 设置 `Homepage URL``https://<your-domain>`
803-
- 设置 `Authorization callback URL``https://<your-domain>/api/auth/callback/github`
802+
1. 登录 [Github Developer](https://github.com/settings/developers) 创建一个新的 OAuth App
803+
2. 生成一个新的 `Client ID``Client Secret`
804+
3. 配置参数:
805+
- `Application name`: `<your-app-name>`
806+
- `Homepage URL`: `https://<your-domain>`
807+
- `Authorization callback URL`: `https://<your-domain>/api/auth/callback/github`
808+
809+
## Google OAuth App 配置
810+
811+
1. 访问 [Google Cloud Console](https://console.cloud.google.com/) 创建项目
812+
2. 配置 OAuth 同意屏幕
813+
3. 创建 OAuth 客户端 ID
814+
- 应用类型:Web 应用
815+
- 已获授权的 Javascript 来源:`https://<your-domain>`
816+
- 已获授权的重定向 URI:`https://<your-domain>/api/auth/callback/google`
817+
4. 获取 `Client ID``Client Secret`
818+
5. 配置环境变量 `AUTH_GOOGLE_ID``AUTH_GOOGLE_SECRET`
819+
804820

805821

806822
## 贡献
807823

808-
欢迎提交 Pull Request 或者 Issue来帮助改进这个项目
824+
欢迎提交 Pull Request 或者 Issue 来帮助改进这个项目
809825

810826
## 许可证
811827

app/components/auth/login-form.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ export function LoginForm({ turnstile }: LoginFormProps) {
200200
signIn("github", { callbackUrl: "/" })
201201
}
202202

203+
const handleGoogleLogin = () => {
204+
signIn("google", { callbackUrl: "/" })
205+
}
206+
203207
return (
204208
<Card className="w-[95%] max-w-lg border-2 border-primary/20">
205209
<CardHeader className="space-y-2">
@@ -297,6 +301,32 @@ export function LoginForm({ turnstile }: LoginFormProps) {
297301
<Github className="mr-2 h-4 w-4" />
298302
{t("actions.githubLogin")}
299303
</Button>
304+
305+
<Button
306+
variant="outline"
307+
className="w-full"
308+
onClick={handleGoogleLogin}
309+
>
310+
<svg className="mr-2 h-4 w-4" viewBox="0 0 24 24">
311+
<path
312+
fill="currentColor"
313+
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
314+
/>
315+
<path
316+
fill="currentColor"
317+
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
318+
/>
319+
<path
320+
fill="currentColor"
321+
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
322+
/>
323+
<path
324+
fill="currentColor"
325+
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
326+
/>
327+
</svg>
328+
{t("actions.googleLogin")}
329+
</Button>
300330
</div>
301331
</TabsContent>
302332
<TabsContent value="register" className="space-y-4 mt-0">

app/components/profile/profile-card.tsx

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,38 @@ const roleConfigs = {
2626
civilian: { key: 'CIVILIAN', icon: User2 },
2727
} as const
2828

29+
const providerConfigs = {
30+
google: {
31+
label: "Google",
32+
className: "text-red-500 bg-red-500/10",
33+
icon: (props: any) => (
34+
<svg viewBox="0 0 24 24" {...props}>
35+
<path
36+
fill="currentColor"
37+
d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
38+
/>
39+
<path
40+
fill="currentColor"
41+
d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
42+
/>
43+
<path
44+
fill="currentColor"
45+
d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
46+
/>
47+
<path
48+
fill="currentColor"
49+
d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
50+
/>
51+
</svg>
52+
),
53+
},
54+
github: {
55+
label: "GitHub",
56+
className: "text-primary bg-primary/10",
57+
icon: Github,
58+
},
59+
} as const
60+
2961
export function ProfileCard({ user }: ProfileCardProps) {
3062
const t = useTranslations("profile.card")
3163
const tAuth = useTranslations("auth.signButton")
@@ -56,15 +88,24 @@ export function ProfileCard({ user }: ProfileCardProps) {
5688
<div className="flex-1 min-w-0">
5789
<div className="flex items-center gap-2">
5890
<h2 className="text-xl font-bold truncate">{user.name}</h2>
59-
{
60-
user.email && (
61-
// 先简单实现,后续再完善
62-
<div className="flex items-center gap-1 text-xs text-primary bg-primary/10 px-2 py-0.5 rounded-full flex-shrink-0">
63-
<Github className="w-3 h-3" />
64-
{tAuth("linked")}
65-
</div>
66-
)
67-
}
91+
{!!user?.providers?.length && (
92+
<div className="flex gap-2">
93+
{user.providers.map((provider) => {
94+
const config = providerConfigs[provider as keyof typeof providerConfigs]
95+
if (!config) return null
96+
const Icon = config.icon
97+
return (
98+
<div
99+
key={provider}
100+
className={`flex items-center gap-1 text-xs px-2 py-0.5 rounded-full flex-shrink-0 ${config.className}`}
101+
>
102+
<Icon className="w-3 h-3" />
103+
{config.label}
104+
</div>
105+
)
106+
})}
107+
</div>
108+
)}
68109
</div>
69110
<p className="text-sm text-muted-foreground truncate mt-1">
70111
{
@@ -78,7 +119,7 @@ export function ProfileCard({ user }: ProfileCardProps) {
78119
const Icon = roleConfig.icon
79120
const roleName = t(`roles.${roleConfig.key}` as any)
80121
return (
81-
<div
122+
<div
82123
key={name}
83124
className="flex items-center gap-1 text-xs bg-primary/10 text-primary px-2 py-0.5 rounded"
84125
title={roleName}
@@ -110,15 +151,15 @@ export function ProfileCard({ user }: ProfileCardProps) {
110151
{canManageWebhook && <ApiKeyPanel />}
111152

112153
<div className="flex flex-col sm:flex-row gap-4 px-1">
113-
<Button
154+
<Button
114155
onClick={() => router.push(`/${locale}/moe`)}
115156
className="gap-2 flex-1"
116157
>
117158
<Mail className="w-4 h-4" />
118159
{tNav("backToMailbox")}
119160
</Button>
120-
<Button
121-
variant="outline"
161+
<Button
162+
variant="outline"
122163
onClick={() => signOut({ callbackUrl: `/${locale}` })}
123164
className="flex-1"
124165
>

app/i18n/messages/en/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"login": "Login",
2222
"register": "Sign Up",
2323
"or": "OR",
24-
"githubLogin": "Login with GitHub"
24+
"githubLogin": "Login with GitHub",
25+
"googleLogin": "Login with Google"
2526
},
2627
"errors": {
2728
"usernameRequired": "Please enter username",

app/i18n/messages/ja/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"login": "ログイン",
2222
"register": "登録",
2323
"or": "または",
24-
"githubLogin": "GitHub アカウントでログイン"
24+
"githubLogin": "GitHub アカウントでログイン",
25+
"googleLogin": "Google アカウントでログイン"
2526
},
2627
"errors": {
2728
"usernameRequired": "ユーザー名を入力してください",

app/i18n/messages/ko/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"login": "로그인",
2222
"register": "회원가입",
2323
"or": "또는",
24-
"githubLogin": "GitHub로 로그인"
24+
"githubLogin": "GitHub로 로그인",
25+
"googleLogin": "Google로 로그인"
2526
},
2627
"errors": {
2728
"usernameRequired": "사용자 이름을 입력해주세요",

app/i18n/messages/zh-CN/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"login": "登录",
2222
"register": "注册",
2323
"or": "或者",
24-
"githubLogin": "使用 GitHub 账号登录"
24+
"githubLogin": "使用 GitHub 账号登录",
25+
"googleLogin": "使用 Google 账号登录"
2526
},
2627
"errors": {
2728
"usernameRequired": "请输入用户名",

app/i18n/messages/zh-TW/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"login": "登入",
2222
"register": "註冊",
2323
"or": "或者",
24-
"githubLogin": "使用 GitHub 帳號登入"
24+
"githubLogin": "使用 GitHub 帳號登入",
25+
"googleLogin": "使用 Google 帳號登入"
2526
},
2627
"errors": {
2728
"usernameRequired": "請輸入使用者名稱",

app/lib/auth.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import NextAuth from "next-auth"
22
import GitHub from "next-auth/providers/github"
3+
import Google from "next-auth/providers/google"
34
import { DrizzleAdapter } from "@auth/drizzle-adapter"
45
import { createDb, Db } from "./db"
56
import { accounts, users, roles, userRoles } from "./schema"
@@ -30,7 +31,7 @@ const getDefaultRole = async (): Promise<Role> => {
3031
) {
3132
return defaultRole as Role
3233
}
33-
34+
3435
return ROLES.CIVILIAN
3536
}
3637

@@ -102,6 +103,12 @@ export const {
102103
GitHub({
103104
clientId: process.env.AUTH_GITHUB_ID,
104105
clientSecret: process.env.AUTH_GITHUB_SECRET,
106+
allowDangerousEmailAccountLinking: true,
107+
}),
108+
Google({
109+
clientId: process.env.AUTH_GOOGLE_ID,
110+
clientSecret: process.env.AUTH_GOOGLE_SECRET,
111+
allowDangerousEmailAccountLinking: true,
105112
}),
106113
CredentialsProvider({
107114
name: "Credentials",
@@ -119,7 +126,7 @@ export const {
119126
let parsedCredentials: AuthSchema
120127
try {
121128
parsedCredentials = authSchema.parse({ username, password, turnstileToken })
122-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
129+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
123130
} catch (error) {
124131
throw new Error("输入格式不正确")
125132
}
@@ -196,7 +203,7 @@ export const {
196203
where: eq(userRoles.userId, session.user.id),
197204
with: { role: true },
198205
})
199-
206+
200207
if (!userRoleRecords.length) {
201208
const defaultRole = await getDefaultRole()
202209
const role = await findOrCreateRole(db, defaultRole)
@@ -208,10 +215,16 @@ export const {
208215
role: role
209216
}]
210217
}
211-
218+
212219
session.user.roles = userRoleRecords.map(ur => ({
213220
name: ur.role.name,
214221
}))
222+
223+
const userAccounts = await db.query.accounts.findMany({
224+
where: eq(accounts.userId, session.user.id),
225+
})
226+
227+
session.user.providers = userAccounts.map(account => account.provider)
215228
}
216229

217230
return session
@@ -224,7 +237,7 @@ export const {
224237

225238
export async function register(username: string, password: string) {
226239
const db = createDb()
227-
240+
228241
const existing = await db.query.users.findFirst({
229242
where: eq(users.username, username)
230243
})
@@ -234,7 +247,7 @@ export async function register(username: string, password: string) {
234247
}
235248

236249
const hashedPassword = await hashPassword(password)
237-
250+
238251
const [user] = await db.insert(users)
239252
.values({
240253
username,

0 commit comments

Comments
 (0)