Skip to content

Commit

Permalink
Add support for storage access API
Browse files Browse the repository at this point in the history
  • Loading branch information
lachiet committed Aug 27, 2022
1 parent bc59cff commit 1381250
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/utils/CoinbasePixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class CoinbasePixel {
*/
private state: 'loading' | 'ready' | 'waiting_for_response' | 'failed' = 'loading';
private debug: boolean;
private hasStorageAccess?: boolean;

private host: string;
private pixelIframe?: HTMLIFrameElement;
Expand Down Expand Up @@ -87,6 +88,13 @@ export class CoinbasePixel {
this.addPixelReadyListener();
this.embedPixel();

// Handle browsers where we need to request access before embedding.
if ('hasStorageAccess' in document) {
document.hasStorageAccess().then((hasAccess) => {
this.hasStorageAccess = hasAccess;
this.log('hasStorageAccess()', hasAccess);
});
}
// Setup a timeout for errors that might stop the window from loading i.e. CSP
setTimeout(() => {
if (this.state !== 'ready') {
Expand Down Expand Up @@ -152,6 +160,18 @@ export class CoinbasePixel {
if (!this.isLoggedIn) {
// Embedded experience opens popup for signin
this.startDirectSignin(openEmbeddedExperience);
} else if (this.hasStorageAccess === false) {
// Explicitly check for false as this means the browser API is available and we have a response.
// Note that this needs to be called in a click handler but the iframe can
// be attached outside of the click context.
document
.requestStorageAccess()
.then(openEmbeddedExperience)
.catch((err) => {
this.state = 'failed';
console.error(err);
throw new Error('Error obtaining store access');
});
} else {
openEmbeddedExperience();
}
Expand Down

1 comment on commit 1381250

@Jayjay1488
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jayjay1488

Please sign in to comment.