feat: sessions endpoint
All checks were successful
Lint Codebase / lint (push) Successful in 1m12s

This commit is contained in:
TheClashFruit 2024-09-07 16:42:52 +02:00
parent a9f943b7e4
commit e4012844a6
Signed by: TheClashFruit
GPG key ID: 09BB24C34C2F3204
2 changed files with 46 additions and 0 deletions

View file

@ -245,6 +245,12 @@ class Database {
return row; return row;
} }
async getSessions(userId: BigInt): Promise<any[]> {
const [ rows ] = await this.mysqlPool!.query('SELECT * FROM user_sessions WHERE user_id = ?', [ userId ]);
return rows as any[];
}
// Meta ---------------- // Meta ----------------
async getTeam(): Promise<(TeamMember | undefined)[]> { async getTeam(): Promise<(TeamMember | undefined)[]> {

View file

@ -0,0 +1,40 @@
import { ErrorResponse, User } from '@/interfaces';
import Database from '@/lib/Database';
import { getAuthenticatedUser } from '@/utils/auth_util';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<any>, // User | ErrorResponse
) {
const db = new Database();
const user = await getAuthenticatedUser(req);
if (!user)
return res.status(401).json({
code: 401,
message: 'Unauthorized'
});
if (req.method === 'GET') {
const sessions = await db.getSessions(user.id);
return res.status(200).json(
sessions.map((session) => ({
id: session.id,
ip: session.ip,
userAgent: session.user_agent,
createdAt: session.created_at,
expiresAt: session.expires_at,
}))
);
}
return res.status(405).json({
code: 405,
message: 'Method Not Allowed'
});
}