Complete sermon management system with Nuxt 4, authentication, SQLite database, QR codes, and Docker deployment
This commit is contained in:
37
server/api/auth/login.post.ts
Normal file
37
server/api/auth/login.post.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { authenticateUser, createJWT } from '~/server/utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody(event)
|
||||
const { username, password } = body
|
||||
|
||||
if (!username || !password) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Username and password are required'
|
||||
})
|
||||
}
|
||||
|
||||
const user = await authenticateUser(username, password)
|
||||
if (!user) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Invalid credentials'
|
||||
})
|
||||
}
|
||||
|
||||
const token = await createJWT(user)
|
||||
|
||||
setCookie(event, 'auth_token', token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'strict',
|
||||
maxAge: 7 * 24 * 60 * 60 // 7 days
|
||||
})
|
||||
|
||||
return {
|
||||
user: {
|
||||
id: user.id,
|
||||
username: user.username
|
||||
}
|
||||
}
|
||||
})
|
||||
6
server/api/auth/logout.post.ts
Normal file
6
server/api/auth/logout.post.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
// Clear the auth cookie
|
||||
deleteCookie(event, 'auth_token')
|
||||
|
||||
return { success: true }
|
||||
})
|
||||
27
server/api/auth/verify.get.ts
Normal file
27
server/api/auth/verify.get.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { verifyJWT } from '~/server/utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const token = getCookie(event, 'auth_token')
|
||||
|
||||
if (!token) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'No authentication token provided'
|
||||
})
|
||||
}
|
||||
|
||||
const payload = await verifyJWT(token)
|
||||
if (!payload) {
|
||||
throw createError({
|
||||
statusCode: 401,
|
||||
statusMessage: 'Invalid authentication token'
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
user: {
|
||||
id: payload.userId,
|
||||
username: payload.username
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user