Skip to content

Commit

Permalink
Merge pull request #110 from athombv/fix/dont-destroy-existing-client
Browse files Browse the repository at this point in the history
Fix/dont destroy existing client
  • Loading branch information
WeeJeWel authored Jan 21, 2025
2 parents 1d1c8e9 + 81f9236 commit fef4130
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions lib/OAuth2Driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,23 @@ class OAuth2Driver extends Homey.Driver {
const onLogin = async ({ username, password }) => {
await client.getTokenByCredentials({ username, password });
const session = await client.onGetOAuth2SessionInformation();
const sessionWasNew = OAuth2SessionId === '$new';

OAuth2SessionId = session.id;
const token = client.getToken();
const { title } = session;
client.destroy();

// replace the temporary client by the final one and save it
client = this.homey.app.createOAuth2Client({
sessionId: session.id,
configId: OAuth2ConfigId,
});
if (sessionWasNew) {
// Destroy the temporary client
client.destroy();

// Replace the temporary client by the final one
client = this.homey.app.createOAuth2Client({
sessionId: session.id,
configId: OAuth2ConfigId,
});
}

client.setTitle({ title });
client.setToken({ token });

Expand Down Expand Up @@ -146,16 +152,22 @@ class OAuth2Driver extends Homey.Driver {
.then(async () => {
// get the client's session info
const session = await client.onGetOAuth2SessionInformation();
const sessionWasNew = OAuth2SessionId === '$new';
OAuth2SessionId = session.id;
const token = client.getToken();
const { title } = session;
client.destroy();

// replace the temporary client by the final one and save it
client = this.homey.app.createOAuth2Client({
sessionId: session.id,
configId: OAuth2ConfigId,
});
if (sessionWasNew) {
// Destroy the temporary client
client.destroy();

// Replace the temporary client by the final one
client = this.homey.app.createOAuth2Client({
sessionId: session.id,
configId: OAuth2ConfigId,
});
}

client.setTitle({ title });
client.setToken({ token });

Expand Down Expand Up @@ -213,7 +225,12 @@ class OAuth2Driver extends Homey.Driver {
OAuth2SessionId = id;
this.log(`Selected session ${OAuth2SessionId}`);

if (OAuth2SessionId !== '$new') {
if (OAuth2SessionId === '$new') {
client = this.homey.app.createOAuth2Client({
sessionId: OAuth2Util.getRandomId(),
configId: OAuth2ConfigId,
});
} else {
client = this.homey.app.getOAuth2Client({
configId: OAuth2ConfigId,
sessionId: OAuth2SessionId,
Expand Down

0 comments on commit fef4130

Please sign in to comment.