user management page could not list registered users
This commit is contained in:
parent
a7f4242ecc
commit
a5e472498e
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
@ -10,6 +11,7 @@ import { Label } from '@/components/ui/label';
|
||||||
import { UserPlus } from 'lucide-react';
|
import { UserPlus } from 'lucide-react';
|
||||||
import { useToast } from "@/hooks/use-toast";
|
import { useToast } from "@/hooks/use-toast";
|
||||||
import { useI18n, useCurrentLocale } from '@/locales/client';
|
import { useI18n, useCurrentLocale } from '@/locales/client';
|
||||||
|
import { registerUser } from '@/app/actions/user';
|
||||||
|
|
||||||
export default function RegisterPage() {
|
export default function RegisterPage() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
@ -29,22 +31,33 @@ export default function RegisterPage() {
|
||||||
|
|
||||||
if (password !== confirmPassword) {
|
if (password !== confirmPassword) {
|
||||||
toast({
|
toast({
|
||||||
title: "Registration Error", // Translate
|
title: t('register.error_title'),
|
||||||
description: "Passwords do not match.", // Translate
|
description: t('register.password_mismatch'),
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
const result = await registerUser({ name, email });
|
||||||
|
|
||||||
|
if (result.success && result.user) {
|
||||||
|
// Mock login after successful registration
|
||||||
localStorage.setItem('isToyShareAuthenticated', 'true');
|
localStorage.setItem('isToyShareAuthenticated', 'true');
|
||||||
|
localStorage.setItem('userEmail', result.user.email);
|
||||||
toast({
|
toast({
|
||||||
title: "Registration Successful", // Translate
|
title: t('register.success_title'),
|
||||||
description: "Your account has been created. Welcome!", // Translate
|
description: t('register.success_description'),
|
||||||
});
|
});
|
||||||
router.push(`/${locale}/dashboard`);
|
router.push(`/${locale}/dashboard`);
|
||||||
|
} else {
|
||||||
|
toast({
|
||||||
|
title: t('register.error_title'),
|
||||||
|
description: result.message,
|
||||||
|
variant: "destructive",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -53,7 +66,7 @@ export default function RegisterPage() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{parts[0]}
|
{parts[0]}
|
||||||
<Link href="/login" className="text-primary font-semibold hover:underline">{parts[1]}</Link>
|
<Link href={`/${locale}/login`} className="text-primary font-semibold hover:underline">{parts[1]}</Link>
|
||||||
{parts[2]}
|
{parts[2]}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
|
||||||
|
'use server';
|
||||||
|
|
||||||
|
import db from '@/lib/db';
|
||||||
|
import type { User } from '@/types';
|
||||||
|
import { randomUUID } from 'crypto';
|
||||||
|
|
||||||
|
interface RegisterUserResult {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
user?: User;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function registerUser(data: { name: string; email: string; password?: string }): Promise<RegisterUserResult> {
|
||||||
|
const { name, email } = data;
|
||||||
|
|
||||||
|
// Basic validation
|
||||||
|
if (!name || !email) {
|
||||||
|
return { success: false, message: 'Name and email are required.' };
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if user already exists
|
||||||
|
const existingUser = db.prepare('SELECT * FROM users WHERE email = ?').get(email);
|
||||||
|
if (existingUser) {
|
||||||
|
return { success: false, message: 'An account with this email already exists.' };
|
||||||
|
}
|
||||||
|
|
||||||
|
const newUser: User = {
|
||||||
|
id: `user-${randomUUID()}`,
|
||||||
|
name,
|
||||||
|
email,
|
||||||
|
role: 'User',
|
||||||
|
avatarUrl: '',
|
||||||
|
bio: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
const stmt = db.prepare(
|
||||||
|
'INSERT INTO users (id, name, email, role, avatarUrl, bio) VALUES (@id, @name, @email, @role, @avatarUrl, @bio)'
|
||||||
|
);
|
||||||
|
|
||||||
|
stmt.run(newUser);
|
||||||
|
|
||||||
|
return { success: true, message: 'User registered successfully.', user: newUser };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Registration error:', error);
|
||||||
|
return { success: false, message: 'An unexpected error occurred during registration.' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -36,6 +36,10 @@ export default {
|
||||||
'register.submit_button': 'Create Account',
|
'register.submit_button': 'Create Account',
|
||||||
'register.loading_button': 'Registering...',
|
'register.loading_button': 'Registering...',
|
||||||
'register.has_account': 'Already have an account? <link>Log in</link>',
|
'register.has_account': 'Already have an account? <link>Log in</link>',
|
||||||
|
'register.error_title': 'Registration Error',
|
||||||
|
'register.password_mismatch': 'Passwords do not match.',
|
||||||
|
'register.success_title': 'Registration Successful',
|
||||||
|
'register.success_description': 'Your account has been created. Welcome!',
|
||||||
'toy_details.back_to_toys': 'Back to All Toys',
|
'toy_details.back_to_toys': 'Back to All Toys',
|
||||||
'toy_details.toy_not_found_title': 'Toy Not Found',
|
'toy_details.toy_not_found_title': 'Toy Not Found',
|
||||||
'toy_details.toy_not_found_description': 'Sorry, the toy you are looking for does not exist or has been removed.',
|
'toy_details.toy_not_found_description': 'Sorry, the toy you are looking for does not exist or has been removed.',
|
||||||
|
|
@ -272,5 +276,3 @@ export default {
|
||||||
'message_detail.message_from_you': 'You',
|
'message_detail.message_from_you': 'You',
|
||||||
'message_detail.message_from_name': 'From {name}',
|
'message_detail.message_from_name': 'From {name}',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -36,6 +36,10 @@ export default {
|
||||||
'register.submit_button': '建立帳戶',
|
'register.submit_button': '建立帳戶',
|
||||||
'register.loading_button': '註冊中...',
|
'register.loading_button': '註冊中...',
|
||||||
'register.has_account': '已經有帳戶了? <link>登入</link>',
|
'register.has_account': '已經有帳戶了? <link>登入</link>',
|
||||||
|
'register.error_title': '註冊錯誤',
|
||||||
|
'register.password_mismatch': '密碼不相符。',
|
||||||
|
'register.success_title': '註冊成功',
|
||||||
|
'register.success_description': '您的帳戶已成功建立,歡迎!',
|
||||||
'toy_details.back_to_toys': '返回所有玩具',
|
'toy_details.back_to_toys': '返回所有玩具',
|
||||||
'toy_details.toy_not_found_title': '找不到玩具',
|
'toy_details.toy_not_found_title': '找不到玩具',
|
||||||
'toy_details.toy_not_found_description': '抱歉,您尋找的玩具不存在或已被移除。',
|
'toy_details.toy_not_found_description': '抱歉,您尋找的玩具不存在或已被移除。',
|
||||||
|
|
@ -272,5 +276,3 @@ export default {
|
||||||
'message_detail.message_from_you': '您',
|
'message_detail.message_from_you': '您',
|
||||||
'message_detail.message_from_name': '來自 {name}',
|
'message_detail.message_from_name': '來自 {name}',
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue