Skip to content

Commit

Permalink
Merge pull request #336 from ZIMkaRU/bugfix/fix-bfx-auth-token-refres…
Browse files Browse the repository at this point in the history
…hing

Fix bfx auth token refreshing
  • Loading branch information
ezewer authored Nov 24, 2023
2 parents 0eb1a6c + 1761984 commit 4825ea7
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions workers/loc.api/sync/authenticator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,19 +1082,25 @@ class Authenticator {
} = user ?? {}
const tokenKey = this._getTokenKeyByEmailField(user)

const userSession = this.userSessions.get(token) ?? {}
const authTokenRefreshInterval = authToken
? this.setupAuthTokenRefreshInterval(user)
: null

this.userSessions.set(
token, {
...user,
authTokenFn: () => {
return this.userSessions.get(token)?.authToken
},
authTokenRefreshInterval,
authTokenInvalidateIntervals: new Map()
}
token,
Object.assign(
userSession,
user,
{
authTokenFn: () => {
return this.userSessions.get(token)?.authToken
},
authTokenRefreshInterval,
authTokenInvalidateIntervals: userSession
?.authTokenInvalidateIntervals ?? new Map()
}
)
)
this.userTokenMapByEmail.set(tokenKey, token)
}
Expand Down Expand Up @@ -1133,9 +1139,8 @@ class Authenticator {
? token
: this.userTokenMapByEmail.get(tokenKey)

this.userTokenMapByEmail.delete(tokenKey)

const session = this.userSessions.get(_token) ?? {}
this.userTokenMapByEmail.delete(this._getTokenKeyByEmailField(session))
const {
authTokenRefreshInterval,
authTokenInvalidateIntervals = new Map()
Expand Down Expand Up @@ -1272,8 +1277,12 @@ class Authenticator {
const { authTokenInvalidateIntervals } = userSession
let count = 0

if (authTokenInvalidateIntervals.has(authToken)) {
return
}

const authTokenInvalidateInterval = setInterval(async () => {
const session = this.userSessions.get(token)
const session = this.userSessions.get(token) ?? userSession

try {
count += 1
Expand All @@ -1288,7 +1297,7 @@ class Authenticator {
} catch (err) {
if (count >= 3) {
clearInterval(authTokenInvalidateInterval)
session.authTokenInvalidateIntervals?.delete(authToken)
session?.authTokenInvalidateIntervals?.delete(authToken)
}

this.logger.debug(err)
Expand Down

0 comments on commit 4825ea7

Please sign in to comment.