Next.js 14 App Router application for managing homophone pun game levels: - Better Auth with Prisma adapter for authentication - MySQL database with Prisma ORM - Level CRUD operations with drag-and-drop reordering - Tencent COS integration for image uploads - shadcn/ui components with Tailwind CSS - TanStack Query for server state management
73 lines
1.6 KiB
TypeScript
73 lines
1.6 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
import { hashPassword } from 'better-auth/crypto'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
async function main() {
|
|
const email = process.env.ADMIN_EMAIL || 'admin@example.com'
|
|
const password = process.env.ADMIN_PASSWORD || 'admin123456'
|
|
|
|
// Check if admin already exists
|
|
const existingUser = await prisma.user.findUnique({
|
|
where: { email },
|
|
include: { accounts: true },
|
|
})
|
|
|
|
if (existingUser) {
|
|
// Update password for existing user
|
|
const hashedPassword = await hashPassword(password)
|
|
|
|
if (existingUser.accounts.length > 0) {
|
|
await prisma.account.update({
|
|
where: { id: existingUser.accounts[0].id },
|
|
data: { password: hashedPassword },
|
|
})
|
|
} else {
|
|
await prisma.account.create({
|
|
data: {
|
|
accountId: email,
|
|
providerId: 'credential',
|
|
userId: existingUser.id,
|
|
password: hashedPassword,
|
|
},
|
|
})
|
|
}
|
|
|
|
console.log(`Admin user password updated: ${email}`)
|
|
return
|
|
}
|
|
|
|
// Hash password using Better Auth's method
|
|
const hashedPassword = await hashPassword(password)
|
|
|
|
// Create admin user
|
|
const user = await prisma.user.create({
|
|
data: {
|
|
email,
|
|
name: 'Admin',
|
|
emailVerified: true,
|
|
},
|
|
})
|
|
|
|
// Create account with password
|
|
await prisma.account.create({
|
|
data: {
|
|
accountId: email,
|
|
providerId: 'credential',
|
|
userId: user.id,
|
|
password: hashedPassword,
|
|
},
|
|
})
|
|
|
|
console.log(`Admin user created: ${email}`)
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|