Skip to content

Commit

Permalink
get back auth code for web app
Browse files Browse the repository at this point in the history
  • Loading branch information
chinchang committed May 10, 2024
1 parent 9be78b9 commit 61d4e97
Showing 1 changed file with 53 additions and 34 deletions.
87 changes: 53 additions & 34 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,77 @@ import { trackEvent } from './analytics';
import { auth } from './firebaseInit';
import { log } from './utils';
import {
GithubAuthProvider,
GoogleAuthProvider,
GithubAuthProvider as ExtensionGithubAuthProvider,
GoogleAuthProvider as ExtensionGoogleAuthProvider,
signOut,
signInWithCredential
} from 'firebase/auth/web-extension';
import {
signInWithPopup,
GithubAuthProvider,
GoogleAuthProvider
} from 'firebase/auth';

export const authh = {
logout() {
signOut(auth);
},
async login(providerName) {
const result = await chrome.runtime.sendMessage({
type: 'firebase-auth',
providerName
});

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;
}

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(() => {
const onSuccess = () => {
trackEvent('fn', 'loggedIn', providerName);
// Save to recommend next time
window.db.local.set({
lastAuthProvider: providerName
});
});
};

if (window.IS_EXTENSION) {
const result = await chrome.runtime.sendMessage({
type: 'firebase-auth',
providerName
});

return signInWithPopup(auth, provider).catch(function (error) {
log(error);
if (error.code === 'auth/account-exists-with-different-credential') {
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;
}

let credential;
switch (providerName) {
case 'google':
credential = ExtensionGoogleAuthProvider.credentialFromResult(result);
break;
case 'github':
credential = ExtensionGithubAuthProvider.credentialFromResult(result);
break;
}
});

// authenticationObject is of the type UserCredentialImpl. Use it to authenticate here
return signInWithCredential(auth, credential).then(onSuccess);
}

var provider;
if (providerName === 'google') {
provider = new GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/userinfo.profile');
} else {
provider = new GithubAuthProvider();
}

return signInWithPopup(auth, provider)
.then(onSuccess)
.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'
);
}
});
}
};

0 comments on commit 61d4e97

Please sign in to comment.