1
0
mirror of https://github.com/chinchang/web-maker.git synced 2025-07-25 07:51:12 +02:00

get back auth code for web app

This commit is contained in:
Kushagra Gour
2024-05-10 13:20:16 +05:30
parent 9be78b9afd
commit 61d4e976e4

View File

@@ -2,17 +2,31 @@ import { trackEvent } from './analytics';
import { auth } from './firebaseInit'; import { auth } from './firebaseInit';
import { log } from './utils'; import { log } from './utils';
import { import {
GithubAuthProvider, GithubAuthProvider as ExtensionGithubAuthProvider,
GoogleAuthProvider, GoogleAuthProvider as ExtensionGoogleAuthProvider,
signOut, signOut,
signInWithCredential signInWithCredential
} from 'firebase/auth/web-extension'; } from 'firebase/auth/web-extension';
import {
signInWithPopup,
GithubAuthProvider,
GoogleAuthProvider
} from 'firebase/auth';
export const authh = { export const authh = {
logout() { logout() {
signOut(auth); signOut(auth);
}, },
async login(providerName) { async login(providerName) {
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({ const result = await chrome.runtime.sendMessage({
type: 'firebase-auth', type: 'firebase-auth',
providerName providerName
@@ -31,23 +45,28 @@ export const authh = {
let credential; let credential;
switch (providerName) { switch (providerName) {
case 'google': case 'google':
credential = GoogleAuthProvider.credentialFromResult(result); credential = ExtensionGoogleAuthProvider.credentialFromResult(result);
break; break;
case 'github': case 'github':
credential = GithubAuthProvider.credentialFromResult(result); credential = ExtensionGithubAuthProvider.credentialFromResult(result);
break; break;
} }
// authenticationObject is of the type UserCredentialImpl. Use it to authenticate here // authenticationObject is of the type UserCredentialImpl. Use it to authenticate here
return signInWithCredential(auth, credential).then(() => { return signInWithCredential(auth, credential).then(onSuccess);
trackEvent('fn', 'loggedIn', providerName); }
// Save to recommend next time
window.db.local.set({
lastAuthProvider: providerName
});
});
return signInWithPopup(auth, provider).catch(function (error) { 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); log(error);
if (error.code === 'auth/account-exists-with-different-credential') { if (error.code === 'auth/account-exists-with-different-credential') {
alert( alert(