mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-03-12 11:02:37 +01:00
refactor(token): simplify token deletion and retrieval logic
- Consolidated query conditions for token deletion and retrieval into a single array for improved readability. - Removed redundant error handling for empty query conditions, as the logic now directly checks for provided parameters. - Enhanced the return statement for the findToken method to streamline the code structure.
This commit is contained in:
parent
a4c6553695
commit
6f4c8ef114
1 changed files with 11 additions and 27 deletions
|
|
@ -47,27 +47,13 @@ export function createTokenMethods(mongoose: typeof import('mongoose')) {
|
||||||
async function deleteTokens(query: TokenQuery): Promise<TokenDeleteResult> {
|
async function deleteTokens(query: TokenQuery): Promise<TokenDeleteResult> {
|
||||||
try {
|
try {
|
||||||
const Token = mongoose.models.Token;
|
const Token = mongoose.models.Token;
|
||||||
const conditions = [];
|
|
||||||
|
|
||||||
if (query.userId) {
|
|
||||||
conditions.push({ userId: query.userId });
|
|
||||||
}
|
|
||||||
if (query.token) {
|
|
||||||
conditions.push({ token: query.token });
|
|
||||||
}
|
|
||||||
if (query.email) {
|
|
||||||
conditions.push({ email: query.email });
|
|
||||||
}
|
|
||||||
if (query.identifier) {
|
|
||||||
conditions.push({ identifier: query.identifier });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (conditions.length === 0) {
|
|
||||||
throw new Error('At least one query parameter must be provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
return await Token.deleteMany({
|
return await Token.deleteMany({
|
||||||
$or: conditions,
|
$or: [
|
||||||
|
{ userId: query.userId },
|
||||||
|
{ token: query.token },
|
||||||
|
{ email: query.email },
|
||||||
|
{ identifier: query.identifier },
|
||||||
|
],
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.debug('An error occurred while deleting tokens:', error);
|
logger.debug('An error occurred while deleting tokens:', error);
|
||||||
|
|
@ -96,13 +82,11 @@ export function createTokenMethods(mongoose: typeof import('mongoose')) {
|
||||||
conditions.push({ identifier: query.identifier });
|
conditions.push({ identifier: query.identifier });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (conditions.length === 0) {
|
const token = await Token.findOne({
|
||||||
throw new Error('At least one query parameter must be provided');
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await Token.findOne({
|
|
||||||
$and: conditions,
|
$and: conditions,
|
||||||
}).lean()) as IToken | null;
|
}).lean();
|
||||||
|
|
||||||
|
return token as IToken | null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.debug('An error occurred while finding token:', error);
|
logger.debug('An error occurred while finding token:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|
@ -111,10 +95,10 @@ export function createTokenMethods(mongoose: typeof import('mongoose')) {
|
||||||
|
|
||||||
// Return all methods
|
// Return all methods
|
||||||
return {
|
return {
|
||||||
|
findToken,
|
||||||
createToken,
|
createToken,
|
||||||
updateToken,
|
updateToken,
|
||||||
deleteTokens,
|
deleteTokens,
|
||||||
findToken,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue