-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor demo to refresh token automatically #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,29 +14,101 @@ | |
color: red; | ||
margin-top: 1rem; | ||
} | ||
#app { | ||
min-height: 200px; | ||
.code { | ||
font-family: monospace; | ||
font-weight: 600; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Airbyte Widget Demo</h1> | ||
<div id="app"> | ||
<h1>Airbyte Widget Demo</h1> | ||
<p> | ||
This is a demo application of the Airbyte Embedded widget. Make sure you have the following environment variables | ||
set in <span class="code">/demo/.env</span> before running it: | ||
<ul> | ||
<li><span class="code">VITE_AB_ORGANIZATION_ID</span> - Your Airbyte organization ID</li> | ||
<li><span class="code">VITE_AB_WORKSPACE_ID</span> - Your Airbyte workspace ID</li> | ||
<li><span class="code">VITE_AB_CLIENT_ID</span> - Your Airbyte client_id</li> | ||
<li><span class="code">VITE_AB_CLIENT_SECRET</span> - Your Airbyte client_secret</li> | ||
</ul> | ||
</p> | ||
<p>This demo uses these environment variables to fetch and refresh a scoped access token for the widget. In a | ||
production application, the <span class="code">client_id</span> and <span class="code">client_secret</span> should not be exposed to your web application.</p> | ||
<div id="loading" class="loading">Loading widget...</div> | ||
<div id="error" class="error"></div> | ||
</div> | ||
<script type="module"> | ||
import { EmbeddedWidget } from "@airbyte-embedded/airbyte-embedded-widget"; | ||
import { EmbeddedWidget } from "../src/EmbeddedWidget.ts"; | ||
|
||
const loadingEl = document.getElementById("loading"); | ||
const errorEl = document.getElementById("error"); | ||
|
||
async function getAdminToken() { | ||
const response = await fetch("https://local.airbyte.dev/api/public/v1/applications/token", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
client_id: import.meta.env.VITE_AB_CLIENT_ID, | ||
client_secret: import.meta.env.VITE_AB_CLIENT_SECRET, | ||
"grant-type": "client_credentials", | ||
}), | ||
}); | ||
const { access_token } = await response.json(); | ||
|
||
return access_token; | ||
} | ||
|
||
async function getScopedToken() { | ||
const access_token = await getAdminToken(); | ||
const response = await fetch("https://local.airbyte.dev/api/public/v1/embedded/widget?debug", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
authorization: `Bearer ${access_token}`, | ||
}, | ||
body: JSON.stringify({ | ||
workspaceId: import.meta.env.VITE_AB_WORKSPACE_ID, | ||
organizationId: import.meta.env.VITE_AB_ORGANIZATION_ID, | ||
allowedOrigin: window.location.origin, | ||
}), | ||
}); | ||
const token = await response.json(); | ||
return token; | ||
} | ||
|
||
async function initializeWidget() { | ||
try { | ||
new EmbeddedWidget({ | ||
token: import.meta.env.VITE_AB_EMBEDDED_TOKEN, | ||
const overriddenToken = await getScopedTokenWithOverrides(); | ||
const widget = new EmbeddedWidget({ | ||
token: overriddenToken, | ||
}); | ||
|
||
async function getScopedTokenWithOverrides() { | ||
const { widgetUrl, token } = await getScopedToken(); | ||
const originalWidgetUrl = new URL(widgetUrl); | ||
const newWidgetUrl = new URL( | ||
`${import.meta.env.VITE_AB_WEBAPP_URL}${originalWidgetUrl.pathname.toString()}?${originalWidgetUrl.searchParams.toString()}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to update this to conditionally use |
||
); | ||
const newEncodedToken = btoa( | ||
JSON.stringify({ | ||
widgetUrl: newWidgetUrl.toString(), | ||
token, | ||
}) | ||
); | ||
return newEncodedToken; | ||
} | ||
|
||
async function refreshToken() { | ||
const token = await getScopedTokenWithOverrides(); | ||
widget.updateToken(token); | ||
console.log("Token refreshed:", token); | ||
} | ||
|
||
window.setInterval(getScopedTokenWithOverrides, 60_000); | ||
|
||
// Hide loading state on success | ||
loadingEl.style.display = "none"; | ||
} catch (error) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,5 +45,6 @@ | |
"onlyBuiltDependencies": [ | ||
"esbuild" | ||
] | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.1336b80b948efd7979218a33ba96d9e4d380e6578144f6319979977deec6e3fe2e0a444b864b3ce2b077dda8adc4d654fee32b9c31868f0acb92da0abcf8ea1c" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion on how we import this... Evan had suggested changing this from directly importing the file to importing it using
@airbyte-embedded
originally... I am guessing so it's more production-like.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I guess this is the tension between "the demo is for dev work" and "the demo is a demo". I find it annoying to have to remember to go change the import path every time I want to work on the
EmbeddedWidget
.What if we just move the current demo app into a
dev
directory, and we keep a more polished demo in thedemo
directory? That would also allow us to keep all our dev niceties (client side token refreshing, import path, overrides) separate from the actual "demo" that operators might spin up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a pr introducing a version with a server that solves the token fetching also. Let's do that instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, I split out the
.updateToken()
method into this PR since your PR does not seem to do that yet: #13