diff --git a/lib/Database.ts b/lib/Database.ts index bdedb55..4bc1422 100644 --- a/lib/Database.ts +++ b/lib/Database.ts @@ -245,6 +245,12 @@ class Database { return row; } + async getSessions(userId: BigInt): Promise { + const [ rows ] = await this.mysqlPool!.query('SELECT * FROM user_sessions WHERE user_id = ?', [ userId ]); + + return rows as any[]; + } + // Meta ---------------- async getTeam(): Promise<(TeamMember | undefined)[]> { diff --git a/pages/api/v1/user/@me/sessions.ts b/pages/api/v1/user/@me/sessions.ts index e69de29..991b537 100644 --- a/pages/api/v1/user/@me/sessions.ts +++ b/pages/api/v1/user/@me/sessions.ts @@ -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, // 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' + }); +} \ No newline at end of file