기능구현중
This commit is contained in:
59
server/api/auth/current-user.get.ts
Normal file
59
server/api/auth/current-user.get.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { getDbSession, refreshSession, getSessionIdFromCookie, deleteSessionCookie, getUserRoles } from '../../utils/session'
|
||||
import { queryOne, execute, query } from '../../utils/db'
|
||||
|
||||
/**
|
||||
* 현재 로그인 사용자 정보 (권한 포함)
|
||||
* GET /api/auth/current-user
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const sessionId = getSessionIdFromCookie(event)
|
||||
|
||||
if (!sessionId) {
|
||||
return { user: null }
|
||||
}
|
||||
|
||||
// DB에서 세션 조회
|
||||
const session = await getDbSession(sessionId)
|
||||
|
||||
if (!session) {
|
||||
// 세션이 만료되었거나 없음 → 쿠키 삭제
|
||||
deleteSessionCookie(event)
|
||||
return { user: null }
|
||||
}
|
||||
|
||||
// 사용자 정보 조회
|
||||
const employee = await queryOne<any>(`
|
||||
SELECT * FROM wr_employee_info
|
||||
WHERE employee_id = $1 AND is_active = true
|
||||
`, [session.employeeId])
|
||||
|
||||
if (!employee) {
|
||||
deleteSessionCookie(event)
|
||||
return { user: null }
|
||||
}
|
||||
|
||||
// 세션 갱신 (Sliding Expiration - 10분 연장)
|
||||
await refreshSession(sessionId)
|
||||
|
||||
// 로그인 이력의 last_active_at도 업데이트
|
||||
if (session.loginHistoryId) {
|
||||
await execute(`
|
||||
UPDATE wr_login_history
|
||||
SET last_active_at = NOW()
|
||||
WHERE history_id = $1
|
||||
`, [session.loginHistoryId])
|
||||
}
|
||||
|
||||
// 사용자 권한 조회
|
||||
const roles = await getUserRoles(employee.employee_id)
|
||||
|
||||
return {
|
||||
user: {
|
||||
employeeId: employee.employee_id,
|
||||
employeeName: employee.employee_name,
|
||||
employeeEmail: employee.employee_email,
|
||||
employeePosition: employee.employee_position,
|
||||
roles // 권한 코드 배열 추가
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user