mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-23 23:11:12 +02:00
get back auth code for web app
This commit is contained in:
87
src/auth.js
87
src/auth.js
@@ -2,58 +2,77 @@ 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 result = await chrome.runtime.sendMessage({
|
const onSuccess = () => {
|
||||||
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(() => {
|
|
||||||
trackEvent('fn', 'loggedIn', providerName);
|
trackEvent('fn', 'loggedIn', providerName);
|
||||||
// Save to recommend next time
|
// Save to recommend next time
|
||||||
window.db.local.set({
|
window.db.local.set({
|
||||||
lastAuthProvider: providerName
|
lastAuthProvider: providerName
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
return signInWithPopup(auth, provider).catch(function (error) {
|
if (window.IS_EXTENSION) {
|
||||||
log(error);
|
const result = await chrome.runtime.sendMessage({
|
||||||
if (error.code === 'auth/account-exists-with-different-credential') {
|
type: 'firebase-auth',
|
||||||
|
providerName
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
result.name === 'FirebaseError' &&
|
||||||
|
result.code === 'auth/account-exists-with-different-credential'
|
||||||
|
) {
|
||||||
alert(
|
alert(
|
||||||
'You have already signed up with the same email using different social login'
|
'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'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user