| 149 | } |
| 150 | |
| 151 | export async function POST(request: Request) { |
| 152 | try { |
| 153 | const { action, data } = await request.json(); |
| 154 | |
| 155 | if (action === 'change_password') { |
| 156 | const { currentPassword, newPassword } = data; |
| 157 | |
| 158 | // Read current .env.local |
| 159 | let envContent = ''; |
| 160 | try { |
| 161 | envContent = fs.readFileSync(ENV_LOCAL_PATH, 'utf-8'); |
| 162 | } catch { |
| 163 | return NextResponse.json({ error: 'Could not read configuration' }, { status: 500 }); |
| 164 | } |
| 165 | |
| 166 | // Verify current password |
| 167 | const storedPassword = |
| 168 | getEnvVar(envContent, ADMIN_PASSWORD_KEY) ?? |
| 169 | getEnvVar(envContent, LEGACY_PASSWORD_KEY); |
| 170 | |
| 171 | if (!storedPassword) { |
| 172 | return NextResponse.json({ error: 'Mission Control password is not configured' }, { status: 500 }); |
| 173 | } |
| 174 | |
| 175 | if (storedPassword !== currentPassword) { |
| 176 | return NextResponse.json({ error: 'Current password is incorrect' }, { status: 401 }); |
| 177 | } |
| 178 | |
| 179 | // Update password |
| 180 | let newEnvContent = setEnvVar(envContent, ADMIN_PASSWORD_KEY, newPassword); |
| 181 | |
| 182 | if (getEnvVar(envContent, LEGACY_PASSWORD_KEY) !== null) { |
| 183 | newEnvContent = setEnvVar(newEnvContent, LEGACY_PASSWORD_KEY, newPassword); |
| 184 | } |
| 185 | |
| 186 | fs.writeFileSync(ENV_LOCAL_PATH, newEnvContent); |
| 187 | process.env.ADMIN_PASSWORD = newPassword; |
| 188 | |
| 189 | return NextResponse.json({ success: true, message: 'Password updated successfully' }); |
| 190 | } |
| 191 | |
| 192 | if (action === 'clear_activity_log') { |
| 193 | const activitiesPath = path.join(process.cwd(), 'data', 'activities.json'); |
| 194 | fs.writeFileSync(activitiesPath, '[]'); |
| 195 | return NextResponse.json({ success: true, message: 'Activity log cleared' }); |
| 196 | } |
| 197 | |
| 198 | return NextResponse.json({ error: 'Unknown action' }, { status: 400 }); |
| 199 | } catch (error) { |
| 200 | return NextResponse.json({ error: 'Action failed' }, { status: 500 }); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | function formatUptime(seconds: number): string { |
| 205 | const days = Math.floor(seconds / 86400); |