diff --git a/packages/core/src/auth/auth.test.ts b/packages/core/src/auth/auth.test.ts new file mode 100644 index 00000000..2e5d57f4 --- /dev/null +++ b/packages/core/src/auth/auth.test.ts @@ -0,0 +1,95 @@ +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest' +import { prisma } from '@shumai/db' +import { setupTestDbHooks } from '@shumai/db/test' +import { auth } from './auth' +import { userMetadataService } from '../user-metadata/user-metadata' +import { teamService } from '@shumai/core/src/team/team' + +describe('Auth Signup Locale Hook', () => { + setupTestDbHooks() + + beforeEach(() => { + vi.spyOn(teamService, 'getSignupInfo').mockResolvedValue({ + initialized: false, + demoMode: false, + userCount: 1, + }) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it('should save locale metadata for the user upon signup', async () => { + const email = `signup-locale-${Date.now()}@example.com` + + // We must use any here because 'locale' is a custom property we extract in + // our auth hooks, but better-auth's signUpEmail typescript interface does + // not recognize it. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const res = await (auth.api.signUpEmail as any)({ + headers: new Headers(), + body: { + email, + password: 'testpassword', + name: 'Locale Tester', + locale: 'zh', + }, + }) + + if (res.error) { + console.error('SIGNUP ERROR:', res.error) + } + + expect(res).toBeDefined() + expect(res.user).toBeDefined() + expect(res.user.email).toBe(email) + + // Check that the userMetadata record was created with key 'locale' and value 'zh' + const teamMembers = await prisma.teamMember.findMany({ + where: { userId: res.user.id }, + }) + expect(teamMembers.length).toBeGreaterThan(0) + + for (const member of teamMembers) { + const meta = await userMetadataService.getMetadata(res.user.id, member.teamId, 'locale') + expect(meta).toBeDefined() + expect(meta?.value).toBe('zh') + } + }) + + it('should not save locale metadata if locale is invalid or missing', async () => { + const email = `signup-no-locale-${Date.now()}@example.com` + + // We must use any here because 'locale' is a custom property we extract in + // our auth hooks, but better-auth's signUpEmail typescript interface does + // not recognize it. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const res = await (auth.api.signUpEmail as any)({ + headers: new Headers(), + body: { + email, + password: 'testpassword', + name: 'No Locale Tester', + locale: 'fr', // invalid locale + }, + }) + + if (res.error) { + console.error('SIGNUP ERROR 2:', res.error) + } + + expect(res).toBeDefined() + expect(res.user).toBeDefined() + + const teamMembers = await prisma.teamMember.findMany({ + where: { userId: res.user.id }, + }) + expect(teamMembers.length).toBeGreaterThan(0) + + for (const member of teamMembers) { + const meta = await userMetadataService.getMetadata(res.user.id, member.teamId, 'locale') + expect(meta).toBeNull() + } + }) +}) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index 36256c6e..3de07d48 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -4,6 +4,7 @@ import { prisma } from '@shumai/db' import { teamService } from '@shumai/core/src/team/team' import { inviteService } from '@shumai/core/src/invite/invite' import { createAuthMiddleware } from 'better-auth/api' +import { userMetadataService } from '@shumai/core/src/user-metadata/user-metadata' export const auth = betterAuth({ database: prismaAdapter(prisma, { @@ -102,6 +103,17 @@ export const auth = betterAuth({ if (inviteCode) { await inviteService.consumeInvite(inviteCode, user.id) } + + const locale = body?.locale + if (locale === 'en' || locale === 'zh') { + const joinedTeams = await prisma.teamMember.findMany({ + where: { userId: user.id }, + select: { teamId: true }, + }) + for (const member of joinedTeams) { + await userMetadataService.upsertMetadata(user.id, member.teamId, 'locale', locale) + } + } } catch (err) { console.error(`[Better Auth Hook] Error in after hook: ${err}`) } diff --git a/packages/webui/messages/zh.json b/packages/webui/messages/zh.json index 70de623c..d78cdce8 100644 --- a/packages/webui/messages/zh.json +++ b/packages/webui/messages/zh.json @@ -424,8 +424,8 @@ "avatar_source": "头像来源", "saving": "保存中", - "auth_tagline": "一个工作区,管理所有创意资产。", - "auth_description": "上传并索引您的文件,使用自定义元数据模式丰富它们,直接在媒体上绘制标注,并收集即时反馈——这一切都在一个为创作者打造的现代工作区中完成。", + "auth_tagline": "一站式创意资产管理协作平台。", + "auth_description": "上传您的创意资产,添加各种类型的自定义元数据,直接在图片或视频上绘制标注和添加评论,——这一切都在Shumai,一个为创作者打造的一站式平台上完成。", "auth_feature_uploads": "即时资产上传 & 高保真媒体播放器", "auth_feature_metadata": "自定义元数据模式 & 画布标注审阅", "auth_feature_teams": "无缝团队工作区 & 安全分享", diff --git a/packages/webui/paraglide/messages/auth_description.js b/packages/webui/paraglide/messages/auth_description.js index a9f54812..ef22be70 100644 --- a/packages/webui/paraglide/messages/auth_description.js +++ b/packages/webui/paraglide/messages/auth_description.js @@ -10,7 +10,7 @@ const en_auth_description = /** @type {(inputs: Auth_DescriptionInputs) => Local }; const zh_auth_description = /** @type {(inputs: Auth_DescriptionInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`上传并索引您的文件,使用自定义元数据模式丰富它们,直接在媒体上绘制标注,并收集即时反馈——这一切都在一个为创作者打造的现代工作区中完成。`) + return /** @type {LocalizedString} */ (`上传您的创意资产,添加各种类型的自定义元数据,直接在图片或视频上绘制标注和添加评论,——这一切都在Shumai,一个为创作者打造的一站式平台上完成。`) }; /** diff --git a/packages/webui/paraglide/messages/auth_tagline.js b/packages/webui/paraglide/messages/auth_tagline.js index 160ab36d..da8db7e1 100644 --- a/packages/webui/paraglide/messages/auth_tagline.js +++ b/packages/webui/paraglide/messages/auth_tagline.js @@ -10,7 +10,7 @@ const en_auth_tagline = /** @type {(inputs: Auth_TaglineInputs) => LocalizedStri }; const zh_auth_tagline = /** @type {(inputs: Auth_TaglineInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`一个工作区,管理所有创意资产。`) + return /** @type {LocalizedString} */ (`一站式创意资产管理协作平台。`) }; /** diff --git a/packages/webui/routes/signup.tsx b/packages/webui/routes/signup.tsx index c5e2cd12..609dab46 100644 --- a/packages/webui/routes/signup.tsx +++ b/packages/webui/routes/signup.tsx @@ -1,20 +1,21 @@ import { client } from '@/ui/api/client' -import { useQuery } from '@tanstack/react-query' +import { AuthLayout } from '@/ui/components/auth-layout' import { Button } from '@/ui/components/ui/button' import { Card, CardDescription, CardFooter, CardHeader, CardTitle } from '@/ui/components/ui/card' +import { ShumaiLogo } from '@/ui/components/ui/icons' import { Input } from '@/ui/components/ui/input' import { Label } from '@/ui/components/ui/label' -import { Loader2, Mail, Lock, ArrowRight, UserPlus } from 'lucide-react' +import { signUp } from '@/ui/lib/auth-client' +import { m } from '@/ui/paraglide/messages.js' +import { getLocale, setLocale } from '@/ui/paraglide/runtime.js' import { useAuthStore } from '@/ui/stores/auth' import { zodResolver } from '@hookform/resolvers/zod' +import { useQuery } from '@tanstack/react-query' import { createFileRoute, redirect, useNavigate } from '@tanstack/react-router' +import { ArrowRight, Loader2, Lock, Mail, UserPlus } from 'lucide-react' +import { useState } from 'react' import { useForm } from 'react-hook-form' import { z } from 'zod' -import { signUp } from '@/ui/lib/auth-client' -import { useState } from 'react' -import { ShumaiLogo } from '@/ui/components/ui/icons' -import { AuthLayout } from '@/ui/components/auth-layout' -import { m } from '@/ui/paraglide/messages.js' const signupSchema = z.object({ email: z.string().email(m.invalid_email_address()), @@ -30,6 +31,15 @@ function SignupPage() { const inviteCode = (search as { inviteCode?: string }).inviteCode const [error, setError] = useState(null) const [loading, setLoading] = useState(false) + const [locale, setLocaleState] = useState<'en' | 'zh'>(() => { + const activeLocale = getLocale() + return activeLocale === 'zh' ? 'zh' : 'en' + }) + + const handleLocaleChange = (newLocale: 'en' | 'zh') => { + setLocaleState(newLocale) + setLocale(newLocale) + } const { data: inviteInfo } = useQuery({ queryKey: ['invite', inviteCode], @@ -71,6 +81,7 @@ function SignupPage() { password: data.password, name: data.email, // Use email as the username inviteCode: data.inviteCode, + locale, // eslint-disable-next-line @typescript-eslint/no-explicit-any } as any) @@ -238,14 +249,38 @@ function SignupPage() { -
- {m.already_have_account()} - - {m.login()} - +
+
+ {m.already_have_account()} + + {m.login()} + +
+ +
+ + / + +
)