1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-31 18:50:14 +02:00

make auth work with correct provider

This commit is contained in:
Kushagra Gour
2024-05-09 16:44:28 +05:30
parent 71582cf608
commit 3482fabefd
5 changed files with 70 additions and 58 deletions

View File

@@ -1,9 +1,9 @@
setTimeout(() => {
console.log('firing postmessag from iframe');
// window.top.postMessage('{"a":2}', "*");
}, 3000);
import { signInWithPopup, GoogleAuthProvider, getAuth } from 'firebase/auth';
import {
signInWithPopup,
GithubAuthProvider,
GoogleAuthProvider,
getAuth
} from 'firebase/auth';
import { initializeApp } from 'firebase/app';
import { config } from './firebaseConfig.js';
@@ -15,21 +15,25 @@ const auth = getAuth();
// You will need this to assign the targetOrigin for postMessage.
const PARENT_FRAME = document.location.ancestorOrigins[0];
// This demo uses the Google auth provider, but any supported provider works.
// Make sure that you enable any provider you want to use in the Firebase Console.
// https://console.firebase.google.com/project/_/authentication/providers
const PROVIDER = new GoogleAuthProvider();
function sendResponse(result) {
globalThis.parent.self.postMessage(JSON.stringify(result), PARENT_FRAME);
}
globalThis.addEventListener('message', function ({ data }) {
if (data.initAuth) {
const { providerName } = data;
let provider;
if (providerName === 'google') {
provider = new GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/userinfo.profile');
} else {
provider = new GithubAuthProvider();
}
// Opens the Google sign-in page in a popup, inside of an iframe in the
// extension's offscreen document.
// To centralize logic, all respones are forwarded to the parent frame,
// which goes on to forward them to the extension's service worker.
signInWithPopup(auth, PROVIDER).then(sendResponse).catch(sendResponse);
signInWithPopup(auth, provider).then(sendResponse).catch(sendResponse);
}
});