Add email field and add it to register logic
This commit is contained in:
parent
6a1823586a
commit
8f8f019ab7
@ -40,10 +40,12 @@ CREATE TABLE `SpriteAction` (
|
|||||||
CREATE TABLE `User` (
|
CREATE TABLE `User` (
|
||||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||||
`username` VARCHAR(191) NOT NULL,
|
`username` VARCHAR(191) NOT NULL,
|
||||||
|
`email` VARCHAR(191) NOT NULL,
|
||||||
`password` VARCHAR(191) NOT NULL,
|
`password` VARCHAR(191) NOT NULL,
|
||||||
`online` BOOLEAN NOT NULL DEFAULT false,
|
`online` BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
|
||||||
UNIQUE INDEX `User_username_key`(`username`),
|
UNIQUE INDEX `User_username_key`(`username`),
|
||||||
|
UNIQUE INDEX `User_email_key`(`email`),
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||||
|
|
@ -1,6 +1,7 @@
|
|||||||
model User {
|
model User {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
username String @unique
|
username String @unique
|
||||||
|
email String @unique
|
||||||
password String
|
password String
|
||||||
online Boolean @default(false)
|
online Boolean @default(false)
|
||||||
characters Character[]
|
characters Character[]
|
||||||
|
@ -27,6 +27,19 @@ class UserRepository {
|
|||||||
throw new Error(`Failed to get user by username: ${error.message}`)
|
throw new Error(`Failed to get user by username: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getByEmail(email: string): Promise<User | null> {
|
||||||
|
try {
|
||||||
|
return await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
email
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle error
|
||||||
|
throw new Error(`Failed to get user by email: ${error.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new UserRepository()
|
export default new UserRepository()
|
||||||
|
@ -33,16 +33,22 @@ class UserService {
|
|||||||
* @param username
|
* @param username
|
||||||
* @param password
|
* @param password
|
||||||
*/
|
*/
|
||||||
async register(username: string, password: string): Promise<boolean | User> {
|
async register(username: string, email: string, password: string): Promise<boolean | User> {
|
||||||
const user = await UserRepository.getByUsername(username)
|
const user = await UserRepository.getByUsername(username)
|
||||||
if (user) {
|
if (user) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const userByEmail = await UserRepository.getByEmail(email)
|
||||||
|
if (userByEmail) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
const hashedPassword = await bcrypt.hash(password, 10)
|
const hashedPassword = await bcrypt.hash(password, 10)
|
||||||
return prisma.user.create({
|
return prisma.user.create({
|
||||||
data: {
|
data: {
|
||||||
username,
|
username,
|
||||||
|
email,
|
||||||
password: hashedPassword
|
password: hashedPassword
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -40,16 +40,16 @@ async function addHttpRoutes(app: Application) {
|
|||||||
* @param res
|
* @param res
|
||||||
*/
|
*/
|
||||||
app.post('/register', async (req: Request, res: Response) => {
|
app.post('/register', async (req: Request, res: Response) => {
|
||||||
const { username, password } = req.body
|
const { username, email, password } = req.body
|
||||||
|
|
||||||
try {
|
try {
|
||||||
registerAccountSchema.parse({ username, password })
|
registerAccountSchema.parse({ username, email, password })
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return res.status(400).json({ message: error.errors[0]?.message })
|
return res.status(400).json({ message: error.errors[0]?.message })
|
||||||
}
|
}
|
||||||
|
|
||||||
const userService = new UserService()
|
const userService = new UserService()
|
||||||
const user = await userService.register(username, password)
|
const user = await userService.register(username, email, password)
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
return res.status(200).json({ message: 'User registered' })
|
return res.status(200).json({ message: 'User registered' })
|
||||||
|
@ -20,6 +20,11 @@ export const registerAccountSchema = z.object({
|
|||||||
.min(3, { message: 'Name must be at least 3 characters long' })
|
.min(3, { message: 'Name must be at least 3 characters long' })
|
||||||
.max(255, { message: 'Name must be at most 255 characters long' })
|
.max(255, { message: 'Name must be at most 255 characters long' })
|
||||||
.regex(/^[A-Za-z][A-Za-z0-9_-]*$/, { message: 'Name must start with a letter and can only contain letters, numbers, underscores, or dashes' }),
|
.regex(/^[A-Za-z][A-Za-z0-9_-]*$/, { message: 'Name must start with a letter and can only contain letters, numbers, underscores, or dashes' }),
|
||||||
|
email: z
|
||||||
|
.string()
|
||||||
|
.min(3, { message: 'Email must be at least 3 characters long' })
|
||||||
|
.max(255, { message: 'Email must be at most 255 characters long' })
|
||||||
|
.regex(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, { message: 'Email must be valid' }),
|
||||||
password: z
|
password: z
|
||||||
.string()
|
.string()
|
||||||
.min(8, {
|
.min(8, {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user