Files
parcer/server/src/routes/proxy.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-10-01 15:28:37 +05:30
import { Router, Request, Response } from 'express';
2024-10-02 23:47:45 +05:30
import User from '../models/User';
2024-10-02 23:48:04 +05:30
import { hashPassword } from '../utils/auth';
2024-10-02 23:49:39 +05:30
import { requireSignIn } from '../middlewares/auth';
2024-10-02 23:47:32 +05:30
2024-10-01 15:28:37 +05:30
export const router = Router();
2024-10-02 23:50:12 +05:30
interface AuthenticatedRequest extends Request {
user?: { id: string };
}
2024-10-02 23:49:39 +05:30
router.post('/config', requireSignIn, async (req: Request, res: Response) => {
2024-10-02 23:47:32 +05:30
const { server_url, username, password } = req.body;
2024-10-01 15:28:37 +05:30
try {
2024-10-02 23:47:32 +05:30
if (!server_url) {
return res.status(400).send('Proxy URL is required');
}
2024-10-02 23:47:45 +05:30
const userId = 1;
2024-10-02 23:47:32 +05:30
const user = await User.findByPk(userId);
if (!user) {
return res.status(404).send('User not found');
2024-10-01 15:28:37 +05:30
}
2024-10-02 23:47:32 +05:30
let hashedProxyUsername: string | null = null;
let hashedProxyPassword: string | null = null;
if (username && password) {
hashedProxyUsername = await hashPassword(username);
hashedProxyPassword = await hashPassword(password);
} else if (username && !password) {
return res.status(400).send('Proxy password is required when proxy username is provided');
}
user.proxy_url = server_url;
user.proxy_username = hashedProxyUsername;
user.proxy_password = hashedProxyPassword;
await user.save();
res.status(200).send('Proxy configuration saved successfully');
2024-10-01 15:28:37 +05:30
} catch (error: any) {
2024-10-02 23:47:32 +05:30
res.status(500).send(`Could not save proxy configuration - ${error.message}`);
2024-10-01 15:28:37 +05:30
}
2024-10-02 23:47:32 +05:30
});