feat: add CALL util.debugAdd
gitea/salix/pipeline/pr-master There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-06-20 11:49:25 +00:00
parent b02a4721af
commit eec160497e
1 changed files with 40 additions and 26 deletions

View File

@ -16,36 +16,50 @@ module.exports = Self => {
accessScopes: ['DEFAULT', 'read:multimedia']}); accessScopes: ['DEFAULT', 'read:multimedia']});
Self.renewToken = async function(ctx) { Self.renewToken = async function(ctx) {
const {accessToken: token} = ctx.req; let accessToken;
let token;
try {
token = ctx.req.accessToken;
const {courtesyTime} = await models.AccessTokenConfig.findOne({
fields: ['courtesyTime']
});
const isNotExceeded = await Self.validateToken(ctx);
if (isNotExceeded)
return token;
const {courtesyTime} = await models.AccessTokenConfig.findOne({ // Schedule to remove current token
fields: ['courtesyTime'] setTimeout(async() => {
}); try {
const isNotExceeded = await Self.validateToken(ctx); const exists = await models.AccessToken.findById(token.id);
if (isNotExceeded) exists && await Self.logout(token.id);
return token; } catch (error) {
// Schedule to remove current token
setTimeout(async() => {
try {
const exists = await models.AccessToken.findById(token.id);
exists && await Self.logout(token.id);
} catch (err) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(err); console.error(error);
} const body = JSON.stringify({err: error, token});
}, courtesyTime * 1000); await handleError(body);
throw new Error(error);
}
}, courtesyTime * 1000);
// Get scopes // Get scopes
let createTokenOptions = {}; let createTokenOptions = {};
const {scopes} = token; const {scopes} = token;
if (scopes) if (scopes)
createTokenOptions = {scopes: [scopes[0]]}; createTokenOptions = {scopes: [scopes[0]]};
// Create new accessToken // Create new accessToken
const user = await Self.findById(token.userId); const user = await Self.findById(token.userId);
const accessToken = await user.accessTokens.create(createTokenOptions); accessToken = await user.accessTokens.create(createTokenOptions);
return {id: accessToken.id, ttl: accessToken.ttl}; return {id: accessToken.id, ttl: accessToken.ttl};
} catch (error) {
const body = JSON.stringify({error: error.message, token, accessToken});
await handleError(body);
throw new Error(error);
}
}; };
}; };
async function handleError(body, tag = 'renewToken') {
await models.Application.rawSql('CALL util.debugAdd(?,?);', [tag, body]);
}