mirror of
https://github.com/ngthanhvu/kr-phim.git
synced 2026-08-10 15:07:46 +00:00
feat: add authentication modal component and user authentication logic
- Implemented AuthModal.vue for user login and registration with form handling. - Created useAuth composable for managing user state and authentication requests. - Added admin route middleware to protect admin routes. - Developed admin login page with authentication checks. - Implemented API endpoints for user management (login, logout, fetch user, register). - Created database migrations for users table and updated movies table. - Added utility functions for password hashing and JWT token management.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { users } from '../../database/schema'
|
||||
import { eq, and } from 'drizzle-orm'
|
||||
import { verifyPassword, signToken } from '../../utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const db = useDb()
|
||||
const body = await readBody(event)
|
||||
|
||||
const email = typeof body.email === 'string' ? body.email.trim().toLowerCase() : ''
|
||||
const password = typeof body.password === 'string' ? body.password : ''
|
||||
|
||||
if (!email || !password) {
|
||||
throw createError({ statusCode: 400, message: 'Email và mật khẩu là bắt buộc' })
|
||||
}
|
||||
|
||||
const result = await db.select().from(users).where(and(eq(users.email, email), eq(users.active, true))).limit(1)
|
||||
if (!result.length) {
|
||||
throw createError({ statusCode: 401, message: 'Email hoặc mật khẩu không đúng' })
|
||||
}
|
||||
|
||||
const user = result[0]
|
||||
const valid = await verifyPassword(password, user.password)
|
||||
if (!valid) {
|
||||
throw createError({ statusCode: 401, message: 'Email hoặc mật khẩu không đúng' })
|
||||
}
|
||||
|
||||
const payload = { id: user.id, email: user.email, role: user.role }
|
||||
const token = signToken(payload)
|
||||
|
||||
setCookie(event, 'token', token, {
|
||||
httpOnly: true,
|
||||
secure: false,
|
||||
sameSite: 'lax',
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
path: '/',
|
||||
})
|
||||
|
||||
return {
|
||||
user: { id: user.id, email: user.email, name: user.name, role: user.role, avatar: user.avatar },
|
||||
token,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user