feat(app): add password auth for settings

This commit is contained in:
ItzCrazyKns
2024-08-02 19:32:38 +05:30
parent c4932c659a
commit fdfe8d1f41
7 changed files with 152 additions and 41 deletions

View File

@@ -9,11 +9,23 @@ import {
getAnthropicApiKey,
getOpenaiApiKey,
updateConfig,
getConfigPassword,
isLibraryEnabled,
isCopilotEnabled,
isDiscoverEnabled,
} from '../config';
const router = express.Router();
router.get('/', async (_, res) => {
router.get('/', async (req, res) => {
const authHeader = req.headers['authorization']?.split(' ')[1];
const password = getConfigPassword();
if (authHeader !== password) {
res.status(401).json({ message: 'Unauthorized' });
return;
}
const config = {};
const [chatModelProviders, embeddingModelProviders] = await Promise.all([
@@ -40,14 +52,30 @@ router.get('/', async (_, res) => {
config['ollamaApiUrl'] = getOllamaApiEndpoint();
config['anthropicApiKey'] = getAnthropicApiKey();
config['groqApiKey'] = getGroqApiKey();
config['isLibraryEnabled'] = isLibraryEnabled();
config['isCopilotEnabled'] = isCopilotEnabled();
config['isDiscoverEnabled'] = isDiscoverEnabled();
res.status(200).json(config);
});
router.post('/', async (req, res) => {
const authHeader = req.headers['authorization']?.split(' ')[1];
const password = getConfigPassword();
if (authHeader !== password) {
res.status(401).json({ message: 'Unauthorized' });
return;
}
const config = req.body;
const updatedConfig = {
GENERAL: {
DISCOVER_ENABLED: config.isDiscoverEnabled,
LIBRARY_ENABLED: config.isLibraryEnabled,
COPILOT_ENABLED: config.isCopilotEnabled,
},
API_KEYS: {
OPENAI: config.openaiApiKey,
GROQ: config.groqApiKey,
@@ -63,4 +91,14 @@ router.post('/', async (req, res) => {
res.status(200).json({ message: 'Config updated' });
});
router.get('/preferences', (_, res) => {
const preferences = {
isLibraryEnabled: isLibraryEnabled(),
isCopilotEnabled: isCopilotEnabled(),
isDiscoverEnabled: isDiscoverEnabled(),
};
res.status(200).json(preferences);
});
export default router;