From 6c92e4290cea06b5b300bd47d2744b7d812a0b5a Mon Sep 17 00:00:00 2001 From: likui628 <90845831+likui628@users.noreply.github.com> Date: Sun, 22 Sep 2024 21:12:31 +0800 Subject: [PATCH] User login --- src/controllers/auth.ts | 8 +++++++- src/routes/v1/auth.ts | 6 ++++++ src/services/auth.ts | 12 ++++++++++++ src/services/index.ts | 1 + src/services/user.ts | 13 +++++++++++++ src/validations/auth.ts | 5 +++++ 6 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/services/auth.ts diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index 778f531..48a4955 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -1,8 +1,14 @@ import { Request, Response } from 'express' import { asyncHandler, successResponse } from '../utils' -import { userService } from '../services' +import { userService, authService } from '../services' export const register = asyncHandler(async (req: Request, res: Response) => { const user = await userService.createUser(req.body) successResponse(res, user, 201) }) + +export const login = asyncHandler(async (req: Request, res: Response) => { + const { email, password } = req.body + const user = await authService.loginUserWithEmailAndPassword(email, password) + successResponse(res, user, 200) +}) diff --git a/src/routes/v1/auth.ts b/src/routes/v1/auth.ts index ad55897..88af51e 100644 --- a/src/routes/v1/auth.ts +++ b/src/routes/v1/auth.ts @@ -11,4 +11,10 @@ router.post( authController.register, ) +router.post( + '/login', + validate(authValidation.userLoginSchema), + authController.login, +) + export default router diff --git a/src/services/auth.ts b/src/services/auth.ts new file mode 100644 index 0000000..78276d4 --- /dev/null +++ b/src/services/auth.ts @@ -0,0 +1,12 @@ +import { userService } from '.' + +export const loginUserWithEmailAndPassword = async ( + email: string, + password: string, +) => { + const user = await userService.getUserByEmail(email) + if (!user || user.password !== password) { + throw new Error('password is incorrect') + } + return user +} diff --git a/src/services/index.ts b/src/services/index.ts index d4159d4..b0cc6b0 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1 +1,2 @@ export * as userService from './user' +export * as authService from './auth' diff --git a/src/services/user.ts b/src/services/user.ts index e5d1b60..5215986 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -16,3 +16,16 @@ export const createUser = async (user: User) => { throw new Error('create user failed') } } + +export const getUserByEmail = async (email: string) => { + try { + const user = await prisma.user.findUnique({ + where: { + email, + }, + }) + return user + } catch (_err: unknown) { + throw new Error('get user failed') + } +} diff --git a/src/validations/auth.ts b/src/validations/auth.ts index d4195d6..c3c6a03 100644 --- a/src/validations/auth.ts +++ b/src/validations/auth.ts @@ -5,3 +5,8 @@ export const userRegisterSchema = z.object({ password: z.string().min(6), name: z.string(), }) + +export const userLoginSchema = userRegisterSchema.pick({ + email: true, + password: true, +})