add login with sessian and cleanup
All checks were successful
Build and Push Docker Images / build (push) Successful in 2m34s

This commit is contained in:
Rene Kievits
2025-10-22 02:07:56 +02:00
parent 673d29b05f
commit 1980e14e88
31 changed files with 830 additions and 68 deletions

View File

@@ -1,12 +1,15 @@
import express, { type Router, type Request, type Response } from 'express'
import express, { type Router, type Response } from 'express'
import { ApiKeyModel } from '../../../models/apikey.model.ts'
import { verifyAccessToken, type AuthRequest } from '../../../middleware/auth.ts'
const router = express.Router()
router.use(verifyAccessToken)
router.get('/', async (_req: Request, res: Response) => {
router.get('/', verifyAccessToken, async (req: AuthRequest, res: Response) => {
try {
const doc = await ApiKeyModel.findOne({})
const userId = req.userId
const doc = await ApiKeyModel.findOne({ userId: userId })
const apiKey = doc?.apiKey || ''
@@ -17,17 +20,19 @@ router.get('/', async (_req: Request, res: Response) => {
}
})
router.post('/', async (req: Request, res: Response) => {
router.post('/', verifyAccessToken, async (req: AuthRequest, res: Response) => {
try {
const { apiKey } = req.body as { apiKey?: string }
const { apiKey } = req.body
if (!apiKey || !apiKey.trim()) {
return res.status(400).json({ error: 'Invalid API key' })
}
console.log(req.body)
const userId = req.userId
await ApiKeyModel.updateOne(
{},
{ $set: { apiKey: apiKey } },
{ upsert: true }
{ $set: { userId: userId, apiKey: apiKey, lastUsed: new Date() } },
{ upsert: true },
)
res.json({ success: true })
@@ -37,9 +42,10 @@ router.post('/', async (req: Request, res: Response) => {
}
})
router.delete('/', async (_req: Request, res: Response) => {
router.delete('/', verifyAccessToken, async (req: AuthRequest, res: Response) => {
try {
const result = await ApiKeyModel.deleteOne({})
const userId = req.userId
const result = await ApiKeyModel.deleteOne({ userId: userId })
if (result.deletedCount === 0) {
console.log('No API key found to delete.')