Skip to content

Commit

Permalink
Adds sw for auth handling in iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpolman committed Aug 1, 2024
1 parent 042f6a6 commit 912ff65
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
21 changes: 21 additions & 0 deletions apps/app/public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
self.addEventListener('install', (event) => {
self.skipWaiting();
});

self.addEventListener('activate', (event) => {
event.waitUntil(self.clients.claim());
});

self.addEventListener('message', (event) => {
if (event.data.action === 'setSession') {
setSession(event.data.session);
}
});

function setSession(session) {
self.clients.matchAll().then((clients) => {
clients.forEach((client) => {
client.postMessage({ type: 'setSession', data: { session } });
});
});
}
13 changes: 13 additions & 0 deletions apps/app/src/stores/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const useAccountStore = defineStore('account', {
isMobileIFrame: window.top !== window.self && isMobileDevice,
isMobileEthereumBrowser: window.ethereum && isMobileDevice,
isNavbarOffcanvasShown: false,
sw: null as ServiceWorkerRegistration | null,
}),
actions: {
setGlobals(config: { activeWalletId: string }) {
Expand Down Expand Up @@ -104,6 +105,11 @@ export const useAccountStore = defineStore('account', {
const fn = authEventMap[event];
if (fn) fn(session);
});

// Listen for popup auth events
this.sw = await navigator.serviceWorker.register('/sw.js');
await navigator.serviceWorker.ready;
navigator.serviceWorker.addEventListener('message', this.onServiceWorkerMessage);
}

// If no slug is provided we're not on a campaign page so we return early
Expand All @@ -116,6 +122,13 @@ export const useAccountStore = defineStore('account', {
this.setConfig(config.poolId, { ...config, origin });
this.setTheme(config);
},
async onServiceWorkerMessage(event: MessageEvent) {
switch (event.data.type) {
case 'setSession':
await this.setSession(event.data.data.session);
break;
}
},
async onSignedIn(session: Session | null) {
if (session) {
await this.setSession(session);
Expand Down
1 change: 1 addition & 0 deletions apps/app/src/types/interfaces/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type TAccountState = {
participants: TParticipant[];
leaderboard: { rank: number; score: number; questEntryCount: number; account: TAccount }[];
isNavbarOffcanvasShown: boolean;
sw: ServiceWorkerRegistration | null;
};

type TWidgetTheme = {
Expand Down
13 changes: 12 additions & 1 deletion apps/app/src/views/SigninRedirect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { supabase } from '../stores/Account';
export default defineComponent({
name: 'ViewSigninRedirect',
Expand All @@ -38,11 +39,21 @@ export default defineComponent({
},
},
// User is only redirected here using auth on an _blank page or popup
mounted() {
async mounted() {
// Check query params for error
if (this.$route.query.error) {
this.error = this.$route.query.error_description;
}
// Start listening for signed_in event
else {
const { data, error } = await supabase.auth.getSession();
if (error) throw new Error(error.message);
navigator.serviceWorker.controller?.postMessage({
action: 'setSession',
session: data.session,
});
}
},
methods: {
onClickClose() {
Expand Down

0 comments on commit 912ff65

Please sign in to comment.