mirror of
https://github.com/chinchang/web-maker.git
synced 2025-10-16 14:06:07 +02:00
make auth work with correct provider
This commit is contained in:
80
src/auth.js
80
src/auth.js
@@ -1,57 +1,59 @@
|
||||
import { trackEvent } from './analytics';
|
||||
import { auth } from './firebaseInit';
|
||||
import { log } from './utils';
|
||||
import {
|
||||
GithubAuthProvider,
|
||||
TwitterAuthProvider,
|
||||
GoogleAuthProvider,
|
||||
signInWithPopup,
|
||||
signOut
|
||||
} from 'firebase/auth';
|
||||
import { log } from './utils';
|
||||
import { signInWithCredential } from 'firebase/auth/web-extension';
|
||||
signOut,
|
||||
signInWithCredential
|
||||
} from 'firebase/auth/web-extension';
|
||||
|
||||
export const authh = {
|
||||
logout() {
|
||||
signOut();
|
||||
signOut(auth);
|
||||
},
|
||||
async login(providerName) {
|
||||
debugger;
|
||||
const authenticationObject = await chrome.runtime.sendMessage({
|
||||
const result = await chrome.runtime.sendMessage({
|
||||
type: 'firebase-auth',
|
||||
provider: providerName
|
||||
providerName
|
||||
});
|
||||
const credential =
|
||||
GoogleAuthProvider.credentialFromResult(authenticationObject);
|
||||
// authenticationObject is of the type UserCredentialImpl. Use it to authenticate here
|
||||
return signInWithCredential(auth, credential);
|
||||
|
||||
var provider;
|
||||
if (providerName === 'facebook') {
|
||||
provider = new FacebookAuthProvider();
|
||||
} else if (providerName === 'twitter') {
|
||||
provider = new TwitterAuthProvider();
|
||||
} else if (providerName === 'google') {
|
||||
provider = new GoogleAuthProvider();
|
||||
provider.addScope('https://www.googleapis.com/auth/userinfo.profile');
|
||||
} else {
|
||||
provider = new GithubAuthProvider();
|
||||
if (
|
||||
result.name === 'FirebaseError' &&
|
||||
result.code === 'auth/account-exists-with-different-credential'
|
||||
) {
|
||||
alert(
|
||||
'You have already signed up with the same email using different social login'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
return signInWithPopup(auth, provider)
|
||||
.then(function () {
|
||||
trackEvent('fn', 'loggedIn', providerName);
|
||||
// Save to recommend next time
|
||||
window.db.local.set({
|
||||
lastAuthProvider: providerName
|
||||
});
|
||||
})
|
||||
.catch(function (error) {
|
||||
log(error);
|
||||
if (error.code === 'auth/account-exists-with-different-credential') {
|
||||
alert(
|
||||
'You have already signed up with the same email using different social login'
|
||||
);
|
||||
}
|
||||
let credential;
|
||||
switch (providerName) {
|
||||
case 'google':
|
||||
credential = GoogleAuthProvider.credentialFromResult(result);
|
||||
break;
|
||||
case 'github':
|
||||
credential = GithubAuthProvider.credentialFromResult(result);
|
||||
break;
|
||||
}
|
||||
|
||||
// authenticationObject is of the type UserCredentialImpl. Use it to authenticate here
|
||||
return signInWithCredential(auth, credential).then(() => {
|
||||
trackEvent('fn', 'loggedIn', providerName);
|
||||
// Save to recommend next time
|
||||
window.db.local.set({
|
||||
lastAuthProvider: providerName
|
||||
});
|
||||
});
|
||||
|
||||
return signInWithPopup(auth, provider).catch(function (error) {
|
||||
log(error);
|
||||
if (error.code === 'auth/account-exists-with-different-credential') {
|
||||
alert(
|
||||
'You have already signed up with the same email using different social login'
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@@ -79,20 +79,22 @@ async function closeOffscreenDocument() {
|
||||
await chrome.offscreen.closeDocument();
|
||||
}
|
||||
|
||||
function getAuth() {
|
||||
function getAuth(providerName) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// sending to offscreen document
|
||||
const auth = await chrome.runtime.sendMessage({
|
||||
type: 'firebase-auth',
|
||||
target: 'offscreen'
|
||||
target: 'offscreen',
|
||||
providerName
|
||||
});
|
||||
auth?.name !== 'FirebaseError' ? resolve(auth) : reject(auth);
|
||||
});
|
||||
}
|
||||
|
||||
async function firebaseAuth() {
|
||||
async function firebaseAuth(providerName) {
|
||||
await setupOffscreenDocument(OFFSCREEN_DOCUMENT_PATH);
|
||||
|
||||
const auth = await getAuth()
|
||||
const auth = await getAuth(providerName)
|
||||
.then(auth => {
|
||||
console.log('User Authenticated', auth);
|
||||
return auth;
|
||||
@@ -115,8 +117,9 @@ async function firebaseAuth() {
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
|
||||
// received from the app
|
||||
if (message.type === 'firebase-auth') {
|
||||
firebaseAuth().then(sendResponse);
|
||||
firebaseAuth(message.providerName).then(sendResponse);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user