Skip to content

Commit

Permalink
Better token renew logging to be able to follow the flow
Browse files Browse the repository at this point in the history
  • Loading branch information
fredli74 committed Oct 1, 2024
1 parent c6a948a commit f38530a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 3 additions & 3 deletions providers/tesla/tesla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 82,7 @@ export class TeslaAPI {

public async renewToken(refresh_token: string): Promise<TeslaToken> {
try {
log(LogLevel.Trace, `renewToken(${refresh_token})`);
log(LogLevel.Trace, `TeslaAPI.renewToken(${refresh_token})`);

// Tesla authAPI expects form data in the body
const formData = new URLSearchParams();
Expand All @@ -91,10 91,10 @@ 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)}`);
log(LogLevel.Trace, `TeslaAPI.renewToken(${refresh_token}) response: ${JSON.stringify(authResponse)}`);
return this.parseTokenResponse(authResponse);
} catch (e) {
console.debug(`TeslaAPI.renewToken error: ${e}`);
console.debug(`TeslaAPI.renewToken(${refresh_token}) error: ${e}`);
throw e;
}
}
Expand Down
9 changes: 8 additions & 1 deletion providers/tesla/tesla-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 46,14 @@ export async function maintainToken(
log(LogLevel.Trace, `Token ${token.access_token} is still valid`);
return token as TeslaToken;
}
log(LogLevel.Trace, `Token ${token.access_token} is invalid, calling renewToken`);

assert(token.refresh_token !== undefined);
if (token.access_token) {
log(LogLevel.Trace, `Token ${token.access_token} is expired, calling renewToken`);
} else {
log(LogLevel.Trace, `Client pre-emptively requrested token refresh on ${token.refresh_token}`);
}

const newToken = await teslaAPI.renewToken(token.refresh_token);
validToken(db, token.refresh_token, newToken);
return newToken;
Expand Down

0 comments on commit f38530a

Please sign in to comment.