Skip to content

Commit 8f9ef13

Browse files
authored
fix(getUserPluginAuthValue): throws error if no user matches (danny-avila#1522)
1 parent ead1c3c commit 8f9ef13

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

api/server/services/PluginService.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@ const PluginAuth = require('~/models/schema/pluginAuthSchema');
22
const { encrypt, decrypt } = require('~/server/utils/');
33
const { logger } = require('~/config');
44

5-
const getUserPluginAuthValue = async (user, authField) => {
5+
/**
6+
* Asynchronously retrieves and decrypts the authentication value for a user's plugin, based on a specified authentication field.
7+
*
8+
* @param {string} userId - The unique identifier of the user for whom the plugin authentication value is to be retrieved.
9+
* @param {string} authField - The specific authentication field (e.g., 'API_KEY', 'URL') whose value is to be retrieved and decrypted.
10+
* @returns {Promise<string|null>} A promise that resolves to the decrypted authentication value if found, or `null` if no such authentication value exists for the given user and field.
11+
*
12+
* The function throws an error if it encounters any issue during the retrieval or decryption process, or if the authentication value does not exist.
13+
*
14+
* @example
15+
* // To get the decrypted value of the 'token' field for a user with userId '12345':
16+
* getUserPluginAuthValue('12345', 'token').then(value => {
17+
* console.log(value);
18+
* }).catch(err => {
19+
* console.error(err);
20+
* });
21+
*
22+
* @throws {Error} Throws an error if there's an issue during the retrieval or decryption process, or if the authentication value does not exist.
23+
* @async
24+
*/
25+
const getUserPluginAuthValue = async (userId, authField) => {
626
try {
7-
const pluginAuth = await PluginAuth.findOne({ user, authField }).lean();
27+
const pluginAuth = await PluginAuth.findOne({ userId, authField }).lean();
828
if (!pluginAuth) {
9-
return null;
29+
throw new Error(`No plugin auth ${authField} found for user ${userId}`);
1030
}
1131

1232
const decryptedValue = decrypt(pluginAuth.value);
1333
return decryptedValue;
1434
} catch (err) {
1535
logger.error('[getUserPluginAuthValue]', err);
16-
return err;
36+
throw err;
1737
}
1838
};
1939

0 commit comments

Comments
 (0)