NAS deployment fixed

This commit is contained in:
aodulov
2025-12-18 13:03:12 +02:00
parent 97b4e5de32
commit e9aec9a65d
14 changed files with 278 additions and 76 deletions

View File

@@ -21,9 +21,15 @@ export class AuthController {
static async login(req: any, res: Response) {
try {
const { email, password } = req.body;
console.log(`[AuthController] Attempting login for: ${email}`);
const result = await AuthService.login(email, password);
console.log(`[AuthController] Login successful for: ${email}`);
return sendSuccess(res, result);
} catch (error: any) {
console.error(`[AuthController] Login failed for: ${req.body?.email}. Error: ${error.message}`);
if (error.message === 'Invalid credentials') {
return sendError(res, error.message, 400);
}

View File

@@ -56,8 +56,16 @@ async function ensureAdminUser() {
where: { role: 'ADMIN' },
});
const { AuthService } = await import('./services/auth.service');
if (existingAdmin) {
console.info(`✅ Admin user already exists (email: ${existingAdmin.email})`);
if (existingAdmin.email === adminEmail) {
console.info(`✅ Admin user already exists (email: ${existingAdmin.email})`);
} else {
console.info(` Admin user exists but with different email: ${existingAdmin.email}. Expected: ${adminEmail}`);
}
// Even if admin exists, ensure exercises are seeded (will skip if already has them)
await AuthService.seedDefaultExercises(existingAdmin.id);
await prisma.$disconnect();
return;
}
@@ -73,7 +81,10 @@ async function ensureAdminUser() {
},
});
console.info(`🛠️ Created default admin user (email: ${admin.email})`);
// Seed exercises for new admin
await AuthService.seedDefaultExercises(admin.id);
console.info(`✅ Admin user created and exercises seeded (email: ${adminEmail})`);
await prisma.$disconnect();
}
@@ -97,7 +108,7 @@ if (process.env.NODE_ENV === 'production') {
const distPath = path.join(__dirname, '../../dist');
app.use(express.static(distPath));
app.get('*', (req, res) => {
app.get('*all', (req, res) => {
if (!req.path.startsWith('/api')) {
res.sendFile(path.join(distPath, 'index.html'));
} else {

View File

@@ -68,20 +68,30 @@ export class AuthService {
});
// Seed default exercises
// Seed default exercises
await this.seedDefaultExercises(user.id);
const token = jwt.sign({ userId: user.id, role: user.role }, JWT_SECRET);
const { password: _, ...userSafe } = user;
return { user: userSafe, token };
}
static async seedDefaultExercises(userId: string) {
try {
// Ensure env is loaded (in case server didn't restart)
// Ensure env is loaded from root (in case server didn't restart)
if (!process.env.DEFAULT_EXERCISES_CSV_PATH) {
dotenv.config({ path: path.resolve(process.cwd(), '.env'), override: true });
if (!process.env.DEFAULT_EXERCISES_CSV_PATH) {
// Try root if CWD is server
dotenv.config({ path: path.resolve(process.cwd(), '../.env'), override: true });
const rootEnv = path.resolve(process.cwd(), '../.env');
if (fs.existsSync(rootEnv)) {
dotenv.config({ path: rootEnv, override: true });
} else {
dotenv.config({ path: path.resolve(process.cwd(), '.env'), override: true });
}
}
const csvPath = process.env.DEFAULT_EXERCISES_CSV_PATH;
if (csvPath) {
// ... logic continues
let resolvedPath = path.resolve(process.cwd(), csvPath);
// Try to handle if resolvedPath doesn't exist but relative to root does (if CWD is server)
@@ -109,7 +119,7 @@ export class AuthService {
if (row.name && row.type) {
exercisesToCreate.push({
userId: user.id,
userId,
name: row.name,
type: row.type,
bodyWeightPercentage: row.bodyWeightPercentage ? parseFloat(row.bodyWeightPercentage) : 0,
@@ -120,9 +130,16 @@ export class AuthService {
}
if (exercisesToCreate.length > 0) {
await prisma.exercise.createMany({
data: exercisesToCreate
});
// Check if exercises already exist for this user to avoid duplicates
const existingCount = await prisma.exercise.count({ where: { userId } });
if (existingCount === 0) {
await prisma.exercise.createMany({
data: exercisesToCreate
});
console.log(`[AuthService] Seeded ${exercisesToCreate.length} exercises for user: ${userId}`);
} else {
console.log(`[AuthService] User ${userId} already has ${existingCount} exercises. Skipping seed.`);
}
}
}
} else {
@@ -134,11 +151,6 @@ export class AuthService {
console.error('[AuthService] Failed to seed default exercises:', error);
// Non-blocking error
}
const token = jwt.sign({ userId: user.id, role: user.role }, JWT_SECRET);
const { password: _, ...userSafe } = user;
return { user: userSafe, token };
}
static async changePassword(userId: string, newPassword: string) {