mirror of
https://github.com/chinchang/web-maker.git
synced 2025-07-23 06:51: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 { 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
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return signInWithPopup(auth, provider).catch(function (error) {
|
||||
log(error);
|
||||
if (error.code === 'auth/account-exists-with-different-credential') {
|
||||
if (window.IS_EXTENSION) {
|
||||
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 = 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