Skip to content

Commit

Permalink
More Tesla token renewal tracing, still don't get the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
fredli74 committed Sep 3, 2024
1 parent 2512dc7 commit e762978
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions providers/tesla/tesla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class TeslaAPI {
formData.append("refresh_token", refresh_token);

const authResponse = (await this.authAPI.post("/oauth2/v3/token", formData.toString())) as any;
log(LogLevel.Trace, `renewToken response: ${JSON.stringify(authResponse)}`);
return this.parseTokenResponse(authResponse);
} catch (e) {
console.debug(`TeslaAPI.renewToken error: ${e}`);
Expand Down
11 changes: 10 additions & 1 deletion providers/tesla/tesla-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export async function maintainToken(
log(LogLevel.Trace, `Token ${token.access_token} is still valid`);
return token as TeslaToken;
}

await db.pg.none(
`UPDATE service_provider SET service_data = jsonb_strip_nulls(service_data || $2) WHERE service_data @> $1;`,
[
{ token: { refresh_token: token.refresh_token } },
{ renewing_token: true },
]
);

log(LogLevel.Trace, `Token ${token.access_token} is invalid, calling renewToken`);
const newToken = await teslaAPI.renewToken(token.refresh_token);
validToken(db, token.refresh_token, newToken);
Expand All @@ -70,7 +79,7 @@ async function validToken(
`UPDATE service_provider SET service_data = jsonb_strip_nulls(service_data || $2) WHERE service_data @> $1 RETURNING *;`,
[
{ token: { refresh_token: oldRefreshToken } },
{ updated: Date.now(), token: newToken, invalid_token: null },
{ updated: Date.now(), token: newToken, invalid_token: null, renewing_token: null },
]
);
for (const s of dblist) {
Expand Down

0 comments on commit e762978

Please sign in to comment.