From ea3a7011e57e924eeb9024129be49667ebfc6f48 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 2 Oct 2024 23:47:32 +0530 Subject: [PATCH] feat: save proxy details to db --- server/src/routes/proxy.ts | 40 ++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts index 2f130bb9..a5bf7996 100644 --- a/server/src/routes/proxy.ts +++ b/server/src/routes/proxy.ts @@ -1,14 +1,42 @@ import { Router, Request, Response } from 'express'; +import User from '../models/User'; +import { hashPassword, comparePassword } from '../utils/auth'; + export const router = Router(); router.post('/config', async (req: Request, res: Response) => { - const { proxyConfig } = req.body; + const { server_url, username, password } = req.body; + try { - if (!proxyConfig) { - return res.status(400).send('Proxy configuration is required'); + if (!server_url) { + return res.status(400).send('Proxy URL is required'); } - console.log(proxyConfig); + + const userId = 1; + const user = await User.findByPk(userId); + + if (!user) { + return res.status(404).send('User not found'); + } + + 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'); } catch (error: any) { - res.status(500).send(`Could not send proxy configuration - ${error.message}`) + res.status(500).send(`Could not save proxy configuration - ${error.message}`); } -}) \ No newline at end of file +});