initiation librodrome
This commit is contained in:
20
server/api/admin/auth/check.get.ts
Normal file
20
server/api/admin/auth/check.get.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export default defineEventHandler((event) => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
if (!config.adminSecret) {
|
||||
throw createError({ statusCode: 503, statusMessage: 'Admin not configured' })
|
||||
}
|
||||
|
||||
const token = getAdminToken(event)
|
||||
if (!token) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Not authenticated' })
|
||||
}
|
||||
|
||||
const payload = verifyToken(token, config.adminSecret)
|
||||
if (!payload) {
|
||||
clearAdminCookie(event)
|
||||
throw createError({ statusCode: 401, statusMessage: 'Invalid or expired token' })
|
||||
}
|
||||
|
||||
return { authenticated: true }
|
||||
})
|
||||
17
server/api/admin/auth/login.post.ts
Normal file
17
server/api/admin/auth/login.post.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
if (!config.adminPassword || !config.adminSecret) {
|
||||
throw createError({ statusCode: 503, statusMessage: 'Admin not configured' })
|
||||
}
|
||||
|
||||
const body = await readBody<{ password?: string }>(event)
|
||||
|
||||
if (!body?.password || body.password !== config.adminPassword) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Invalid password' })
|
||||
}
|
||||
|
||||
setAdminCookie(event, config.adminSecret)
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
4
server/api/admin/auth/logout.post.ts
Normal file
4
server/api/admin/auth/logout.post.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler((event) => {
|
||||
clearAdminCookie(event)
|
||||
return { ok: true }
|
||||
})
|
||||
Reference in New Issue
Block a user