| const PluginAuth = require('~/models/schema/pluginAuthSchema'); |
| const { encrypt, decrypt } = require('~/server/utils/'); |
| const { logger } = require('~/config'); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const getUserPluginAuthValue = async (userId, authField) => { |
| try { |
| const pluginAuth = await PluginAuth.findOne({ userId, authField }).lean(); |
| if (!pluginAuth) { |
| throw new Error(`No plugin auth ${authField} found for user ${userId}`); |
| } |
|
|
| const decryptedValue = decrypt(pluginAuth.value); |
| return decryptedValue; |
| } catch (err) { |
| logger.error('[getUserPluginAuthValue]', err); |
| throw err; |
| } |
| }; |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| const updateUserPluginAuth = async (userId, authField, pluginKey, value) => { |
| try { |
| const encryptedValue = encrypt(value); |
| const pluginAuth = await PluginAuth.findOne({ userId, authField }).lean(); |
| if (pluginAuth) { |
| const pluginAuth = await PluginAuth.updateOne( |
| { userId, authField }, |
| { $set: { value: encryptedValue } }, |
| ); |
| return pluginAuth; |
| } else { |
| const newPluginAuth = await new PluginAuth({ |
| userId, |
| authField, |
| value: encryptedValue, |
| pluginKey, |
| }); |
| await newPluginAuth.save(); |
| return newPluginAuth; |
| } |
| } catch (err) { |
| logger.error('[updateUserPluginAuth]', err); |
| return err; |
| } |
| }; |
|
|
| const deleteUserPluginAuth = async (userId, authField, all = false) => { |
| if (all) { |
| try { |
| const response = await PluginAuth.deleteMany({ userId }); |
| return response; |
| } catch (err) { |
| logger.error('[deleteUserPluginAuth]', err); |
| return err; |
| } |
| } |
|
|
| try { |
| return await PluginAuth.deleteOne({ userId, authField }); |
| } catch (err) { |
| logger.error('[deleteUserPluginAuth]', err); |
| return err; |
| } |
| }; |
|
|
| module.exports = { |
| getUserPluginAuthValue, |
| updateUserPluginAuth, |
| deleteUserPluginAuth, |
| }; |
|
|