Skip to content

Commit

Permalink
deploy: 4e96236
Browse files Browse the repository at this point in the history
  • Loading branch information
WeeJeWel committed Jan 21, 2025
1 parent 27bf6b5 commit c29e5a9
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions OAuth2Driver.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ <h1 class="text-preset-heading-1">
class OAuth2Driver extends Homey.Driver {

static OAUTH2_CONFIG_ID = 'default';
static OAUTH2_NEW_SESSION_TITLE = 'New User';
static OAUTH2_NEW_SESSION_ICON = null;

/**
* @returns {Promise&lt;void>}
Expand Down Expand Up @@ -230,17 +232,23 @@ <h1 class="text-preset-heading-1">
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 @@ -268,16 +276,22 @@ <h1 class="text-preset-heading-1">
.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 @@ -316,7 +330,8 @@ <h1 class="text-preset-heading-1">
});

result.push({
name: 'New User',
name: this.constructor.OAUTH2_NEW_SESSION_TITLE,
icon: this.constructor.OAUTH2_NEW_SESSION_ICON,
data: {
id: '$new',
},
Expand All @@ -335,7 +350,12 @@ <h1 class="text-preset-heading-1">
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 c29e5a9

Please sign in to comment.