#6264 - FIX - Log renewToken when not success #2619

Merged
jsegarra merged 4 commits from 6264_log_tmp_renewToken into master 2024-06-21 05:17:19 +00:00
1 changed files with 10 additions and 9 deletions
Showing only changes of commit ba48d93c1e - Show all commits

View File

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