Verbessert die Protokollierung in den Club-Controller- und Benutzer-Utils-Dateien, indem die Konsolenausgaben durch eine bedingte Entwicklungsprotokollierungsfunktion ersetzt werden. Aktualisiert die Fehlerbehandlung, um detailliertere Fehlermeldungen auszugeben.
This commit is contained in:
@@ -7,6 +7,9 @@ import HttpError from '../exceptions/HttpError.js';
|
||||
import { config } from 'dotenv';
|
||||
config(); // sorgt dafür, dass process.env.JWT_SECRET geladen wird
|
||||
|
||||
const isDev = process.env.STAGE === 'dev';
|
||||
const devLog = (...args) => isDev && console.log(...args);
|
||||
|
||||
export const getUserByToken = async (token) => {
|
||||
try {
|
||||
// 1. JWT validieren
|
||||
@@ -41,7 +44,7 @@ export const getUserByToken = async (token) => {
|
||||
|
||||
export const hasUserClubAccess = async (userId, clubId) => {
|
||||
try {
|
||||
console.log('[hasUserClubAccess]');
|
||||
devLog('[hasUserClubAccess]');
|
||||
const userClub = await UserClub.findOne({
|
||||
where: {
|
||||
user_id: userId,
|
||||
@@ -51,10 +54,9 @@ export const hasUserClubAccess = async (userId, clubId) => {
|
||||
});
|
||||
return userClub !== null;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.error('[hasUserClubAccess] - error:', error);
|
||||
throw new HttpError('notfound', 500);
|
||||
}
|
||||
console.log('---- no user found');
|
||||
}
|
||||
|
||||
export const checkAccess = async (userToken, clubId) => {
|
||||
@@ -62,11 +64,11 @@ export const checkAccess = async (userToken, clubId) => {
|
||||
const user = await getUserByToken(userToken);
|
||||
const hasAccess = await hasUserClubAccess(user.id, clubId);
|
||||
if (!hasAccess) {
|
||||
console.log('no club access');
|
||||
devLog('[checkAccess] - no club access');
|
||||
throw new HttpError('noaccess', 403);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
devLog('[checkAccess] - error:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
@@ -76,7 +78,7 @@ export const checkGlobalAccess = async (userToken) => {
|
||||
const user = await getUserByToken(userToken);
|
||||
return user; // Einfach den User zurückgeben, da globale Zugriffe nur Authentifizierung benötigen
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
devLog('[checkGlobalAccess] - error:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user