mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Implement centralized logging with RMABLogger
Replaces scattered console statements with a unified RMABLogger across backend API routes and services. Adds LOG_LEVEL-based filtering, job-aware database persistence, and context-based logging. Updates documentation to describe the new logging system and usage patterns. Also documents qBittorrent CSRF header fix
This commit is contained in:
@@ -9,6 +9,9 @@ import { getEncryptionService } from '@/lib/services/encryption.service';
|
||||
import { getConfigService } from '@/lib/services/config.service';
|
||||
import { generateAccessToken, generateRefreshToken } from '@/lib/utils/jwt';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.PlexCallback');
|
||||
|
||||
/**
|
||||
* GET /api/auth/plex/callback?pinId=12345
|
||||
@@ -52,7 +55,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
// Validate user info
|
||||
if (!plexUser || !plexUser.id) {
|
||||
console.error('[Plex OAuth] Invalid user info received:', plexUser);
|
||||
logger.error('Invalid user info received', { plexUser });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'OAuthError',
|
||||
@@ -64,7 +67,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
if (!plexUser.username) {
|
||||
console.error('[Plex OAuth] Username missing from Plex user:', plexUser);
|
||||
logger.error('Username missing from Plex user', { plexUser });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'OAuthError',
|
||||
@@ -84,7 +87,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
// Verify server is configured
|
||||
if (!plexConfig.serverUrl || !plexConfig.authToken) {
|
||||
console.error('[Plex OAuth] Server not configured');
|
||||
logger.error('Server not configured');
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'ConfigurationError',
|
||||
@@ -99,7 +102,7 @@ export async function GET(request: NextRequest) {
|
||||
const serverMachineId = plexConfig.machineIdentifier;
|
||||
|
||||
if (!serverMachineId) {
|
||||
console.error('[Plex OAuth] machineIdentifier not found in configuration');
|
||||
logger.error('machineIdentifier not found in configuration');
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'ConfigurationError',
|
||||
@@ -109,7 +112,7 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
console.log('[Plex OAuth] Using stored machineIdentifier:', serverMachineId);
|
||||
logger.debug('Using stored machineIdentifier', { serverMachineId });
|
||||
|
||||
// SECURITY: Verify user has access to the configured Plex server
|
||||
// This checks if the server appears in the user's list of accessible servers from plex.tv
|
||||
@@ -121,7 +124,7 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
|
||||
if (!hasAccess) {
|
||||
console.warn('[Plex OAuth] User attempted to authenticate without server access:', {
|
||||
logger.warn('User attempted to authenticate without server access', {
|
||||
plexId: plexIdString,
|
||||
username: plexUser.username,
|
||||
serverMachineId,
|
||||
@@ -135,16 +138,16 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
console.log('[Plex OAuth] User verified with server access:', plexUser.username);
|
||||
logger.info('User verified with server access', { username: plexUser.username });
|
||||
|
||||
// Check for Plex Home profiles
|
||||
const homeUsers = await plexService.getHomeUsers(authToken);
|
||||
console.log('[Plex OAuth] Found home users:', homeUsers.length);
|
||||
logger.debug('Found home users', { count: homeUsers.length });
|
||||
|
||||
// If multiple home users exist, redirect to profile selection
|
||||
// (Only show selection if there's more than just the main account)
|
||||
if (homeUsers.length > 1) {
|
||||
console.log('[Plex OAuth] Account has multiple home profiles, redirecting to profile selection');
|
||||
logger.info('Account has multiple home profiles, redirecting to profile selection');
|
||||
|
||||
// Detect if this is a browser request (mobile redirect) vs AJAX (desktop popup polling)
|
||||
const accept = request.headers.get('accept') || '';
|
||||
@@ -157,7 +160,7 @@ export async function GET(request: NextRequest) {
|
||||
(process.env.NODE_ENV === 'production' ? 'https' : 'http');
|
||||
const selectProfileUrl = `${protocol}://${host}/auth/select-profile?pinId=${pinId}`;
|
||||
|
||||
console.log('[Plex OAuth] Redirecting to profile selection:', selectProfileUrl);
|
||||
logger.debug('Redirecting to profile selection', { selectProfileUrl });
|
||||
|
||||
// Return HTML page with JavaScript to store token in sessionStorage and redirect
|
||||
const html = `
|
||||
@@ -197,7 +200,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[Plex OAuth] Single profile or no additional profiles, continuing with main account authentication');
|
||||
logger.debug('Single profile or no additional profiles, continuing with main account authentication');
|
||||
|
||||
// No home users - continue with normal authentication flow using main account
|
||||
// Check if this is the first user (should be promoted to admin)
|
||||
@@ -248,8 +251,8 @@ export async function GET(request: NextRequest) {
|
||||
(process.env.NODE_ENV === 'production' ? 'https' : 'http');
|
||||
const redirectUrl = `${protocol}://${host}/login?auth=success`;
|
||||
|
||||
console.log('[Plex OAuth] Setting cookies for mobile auth...');
|
||||
console.log('[Plex OAuth] Redirect URL:', redirectUrl);
|
||||
logger.debug('Setting cookies for mobile auth');
|
||||
logger.debug('Redirect URL', { redirectUrl });
|
||||
|
||||
// Prepare user data
|
||||
const userDataJson = JSON.stringify({
|
||||
@@ -260,7 +263,7 @@ export async function GET(request: NextRequest) {
|
||||
role: user.role,
|
||||
avatarUrl: user.avatarUrl,
|
||||
});
|
||||
console.log('[Plex OAuth] Setting userData cookie:', userDataJson);
|
||||
logger.debug('Setting userData cookie', { userDataJson });
|
||||
|
||||
// Prepare auth data to pass via URL hash (fallback for mobile browsers that block cookies)
|
||||
const authData = {
|
||||
@@ -331,7 +334,7 @@ export async function GET(request: NextRequest) {
|
||||
path: '/',
|
||||
});
|
||||
|
||||
console.log('[Plex OAuth] Cookies set successfully, returning HTML redirect to:', redirectUrl);
|
||||
logger.debug('Cookies set successfully, returning HTML redirect', { redirectUrl });
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -351,7 +354,7 @@ export async function GET(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to complete Plex OAuth:', error);
|
||||
logger.error('Failed to complete Plex OAuth', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'OAuthError',
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getPlexService } from '@/lib/integrations/plex.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Auth.Plex.HomeUsers');
|
||||
|
||||
/**
|
||||
* GET /api/auth/plex/home-users
|
||||
@@ -32,7 +35,7 @@ export async function GET(request: NextRequest) {
|
||||
users,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to get home users:', error);
|
||||
logger.error('Failed to get home users', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'ServerError',
|
||||
|
||||
@@ -5,6 +5,9 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getPlexService } from '@/lib/integrations/plex.service';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.Auth.PlexLogin');
|
||||
|
||||
/**
|
||||
* POST /api/auth/plex/login
|
||||
@@ -33,7 +36,7 @@ export async function POST(request: NextRequest) {
|
||||
authUrl,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to initiate Plex OAuth:', error);
|
||||
logger.error('Failed to initiate Plex OAuth', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'OAuthError',
|
||||
|
||||
@@ -8,6 +8,9 @@ import { getPlexService } from '@/lib/integrations/plex.service';
|
||||
import { getEncryptionService } from '@/lib/services/encryption.service';
|
||||
import { generateAccessToken, generateRefreshToken } from '@/lib/utils/jwt';
|
||||
import { prisma } from '@/lib/db';
|
||||
import { RMABLogger } from '@/lib/utils/logger';
|
||||
|
||||
const logger = RMABLogger.create('API.PlexSwitchProfile');
|
||||
|
||||
/**
|
||||
* POST /api/auth/plex/switch-profile
|
||||
@@ -77,7 +80,7 @@ export async function POST(request: NextRequest) {
|
||||
profileUsername = profileInfo.friendlyName || `User ${userId}`;
|
||||
profileEmail = profileInfo.email || null;
|
||||
profileThumb = profileInfo.thumb || null;
|
||||
console.log('[Profile Switch] Using provided profile info:', {
|
||||
logger.debug('Using provided profile info', {
|
||||
plexId: profilePlexId,
|
||||
username: profileUsername,
|
||||
});
|
||||
@@ -86,7 +89,7 @@ export async function POST(request: NextRequest) {
|
||||
const profileUser = await plexService.getUserInfo(profileToken);
|
||||
|
||||
if (!profileUser || !profileUser.id) {
|
||||
console.error('[Profile Switch] Failed to get profile user info');
|
||||
logger.error('Failed to get profile user info');
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'ServerError',
|
||||
@@ -100,7 +103,7 @@ export async function POST(request: NextRequest) {
|
||||
profileUsername = profileUser.username || `User ${userId}`;
|
||||
profileEmail = profileUser.email || null;
|
||||
profileThumb = profileUser.thumb || null;
|
||||
console.log('[Profile Switch] Using getUserInfo data:', {
|
||||
logger.debug('Using getUserInfo data', {
|
||||
plexId: profilePlexId,
|
||||
username: profileUsername,
|
||||
});
|
||||
@@ -134,7 +137,7 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[Profile Switch] User authenticated:', {
|
||||
logger.info('User authenticated', {
|
||||
id: user.id,
|
||||
plexId: user.plexId,
|
||||
username: user.plexUsername,
|
||||
@@ -167,7 +170,7 @@ export async function POST(request: NextRequest) {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to switch profile:', error);
|
||||
logger.error('Failed to switch profile', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'ServerError',
|
||||
|
||||
Reference in New Issue
Block a user