추가
This commit is contained in:
32
backend/api/auth/current-user.get.ts
Normal file
32
backend/api/auth/current-user.get.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { queryOne } from '../../utils/db'
|
||||
|
||||
/**
|
||||
* 현재 로그인 사용자 정보
|
||||
* GET /api/auth/current-user
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const userId = getCookie(event, 'user_id')
|
||||
|
||||
if (!userId) {
|
||||
return { user: null }
|
||||
}
|
||||
|
||||
const employee = await queryOne<any>(`
|
||||
SELECT * FROM wr_employee_info
|
||||
WHERE employee_id = $1 AND is_active = true
|
||||
`, [parseInt(userId)])
|
||||
|
||||
if (!employee) {
|
||||
deleteCookie(event, 'user_id')
|
||||
return { user: null }
|
||||
}
|
||||
|
||||
return {
|
||||
user: {
|
||||
employeeId: employee.employee_id,
|
||||
employeeName: employee.employee_name,
|
||||
employeeEmail: employee.employee_email,
|
||||
employeePosition: employee.employee_position
|
||||
}
|
||||
}
|
||||
})
|
||||
62
backend/api/auth/login.post.ts
Normal file
62
backend/api/auth/login.post.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { query, insertReturning, execute } from '../../utils/db'
|
||||
|
||||
interface LoginBody {
|
||||
email: string
|
||||
name: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 이메일+이름 로그인 (임시)
|
||||
* POST /api/auth/login
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<LoginBody>(event)
|
||||
|
||||
if (!body.email || !body.name) {
|
||||
throw createError({ statusCode: 400, message: '이메일과 이름을 입력해주세요.' })
|
||||
}
|
||||
|
||||
// 이메일 형식 검증
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
||||
if (!emailRegex.test(body.email)) {
|
||||
throw createError({ statusCode: 400, message: '올바른 이메일 형식이 아닙니다.' })
|
||||
}
|
||||
|
||||
// 기존 사원 조회
|
||||
let employee = await query<any>(`
|
||||
SELECT * FROM wr_employee_info WHERE employee_email = $1
|
||||
`, [body.email.toLowerCase()])
|
||||
|
||||
let employeeData = employee[0]
|
||||
|
||||
// 없으면 자동 등록
|
||||
if (!employeeData) {
|
||||
employeeData = await insertReturning(`
|
||||
INSERT INTO wr_employee_info (employee_name, employee_email)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *
|
||||
`, [body.name, body.email.toLowerCase()])
|
||||
}
|
||||
|
||||
// 로그인 이력 추가
|
||||
await execute(`
|
||||
INSERT INTO wr_login_history (employee_id) VALUES ($1)
|
||||
`, [employeeData.employee_id])
|
||||
|
||||
// 쿠키에 사용자 정보 저장 (간단한 임시 세션)
|
||||
setCookie(event, 'user_id', String(employeeData.employee_id), {
|
||||
httpOnly: true,
|
||||
maxAge: 60 * 60 * 24 * 7, // 7일
|
||||
path: '/'
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
employeeId: employeeData.employee_id,
|
||||
employeeName: employeeData.employee_name,
|
||||
employeeEmail: employeeData.employee_email,
|
||||
employeePosition: employeeData.employee_position
|
||||
}
|
||||
}
|
||||
})
|
||||
8
backend/api/auth/logout.post.ts
Normal file
8
backend/api/auth/logout.post.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* 로그아웃
|
||||
* POST /api/auth/logout
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
deleteCookie(event, 'user_id')
|
||||
return { success: true }
|
||||
})
|
||||
23
backend/api/auth/recent-users.get.ts
Normal file
23
backend/api/auth/recent-users.get.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { query } from '../../utils/db'
|
||||
|
||||
/**
|
||||
* 최근 로그인 사용자 목록
|
||||
* GET /api/auth/recent-users
|
||||
*/
|
||||
export default defineEventHandler(async () => {
|
||||
const users = await query(`
|
||||
SELECT * FROM wr_recent_login_users
|
||||
ORDER BY last_active_at DESC
|
||||
LIMIT 10
|
||||
`)
|
||||
|
||||
return {
|
||||
users: users.map((u: any) => ({
|
||||
employeeId: u.employee_id,
|
||||
employeeName: u.employee_name,
|
||||
employeeEmail: u.employee_email,
|
||||
employeePosition: u.employee_position,
|
||||
lastActiveAt: u.last_active_at
|
||||
}))
|
||||
}
|
||||
})
|
||||
49
backend/api/auth/select-user.post.ts
Normal file
49
backend/api/auth/select-user.post.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { queryOne, execute } from '../../utils/db'
|
||||
|
||||
interface SelectUserBody {
|
||||
employeeId: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 기존 사용자 선택 로그인
|
||||
* POST /api/auth/select-user
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<SelectUserBody>(event)
|
||||
|
||||
if (!body.employeeId) {
|
||||
throw createError({ statusCode: 400, message: '사용자를 선택해주세요.' })
|
||||
}
|
||||
|
||||
// 사원 조회
|
||||
const employee = await queryOne<any>(`
|
||||
SELECT * FROM wr_employee_info
|
||||
WHERE employee_id = $1 AND is_active = true
|
||||
`, [body.employeeId])
|
||||
|
||||
if (!employee) {
|
||||
throw createError({ statusCode: 404, message: '사용자를 찾을 수 없습니다.' })
|
||||
}
|
||||
|
||||
// 로그인 이력 추가
|
||||
await execute(`
|
||||
INSERT INTO wr_login_history (employee_id) VALUES ($1)
|
||||
`, [employee.employee_id])
|
||||
|
||||
// 쿠키 설정
|
||||
setCookie(event, 'user_id', String(employee.employee_id), {
|
||||
httpOnly: true,
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
path: '/'
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
employeeId: employee.employee_id,
|
||||
employeeName: employee.employee_name,
|
||||
employeeEmail: employee.employee_email,
|
||||
employeePosition: employee.employee_position
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user