Untitled

 avatar
unknown
plain_text
2 years ago
4.6 kB
4
Indexable
// Import the functions you need from the SDKs you need

// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration

//---------Replace This with your Own SDK-------->

try {
  self.importScripts(
    'firebase/firebase-app-compat.js',
    'firebase/firebase-auth-compat.js',
    'firebase/firebase-database-compat.js'
  );
  var firebaseConfig = {
    apiKey: "AIzaSyCttg4sdgCTaAJfsnDtCnAKNPHDkFIE58g",
    authDomain: "fir-testapp-52802.firebaseapp.com",
    projectId: "fir-testapp-52802",
    storageBucket: "fir-testapp-52802.appspot.com",
    messagingSenderId: "418198983386",
    appId: "1:418198983386:web:10d7b1df9238f821078be3",
    //databaseURL: "https://fir-testapp-52802-default-rtdb.europe-west1.firebasedatabase.app/",
  };
  firebase.initializeApp(firebaseConfig);

  var user = firebase.auth().currentUser;
  console.log(user);
  if (user) {
    //user is signed in
    chrome.storage.local.set({ authInfo: user });
  } else {
    console.log("User is not signed in");
    //user is not signed in
    chrome.storage.local.set({ authInfo: false });
  }

  chrome.runtime.onMessage.addListener((msg, sender, resp) => {
    console.log("Message Received");
    if(msg.command == "user-auth") {


      firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
          // User is signed in.
          chrome.storage.local.set({ authInfo: user });
          resp({type: "result", status: "success", data: user, userObj: user});
        } else {
          // No user is signed in.
          chrome.storage.local.set({ authInfo: false });
          resp({type: "result", status: "error", data: false});
        }
      });
    }

    if(msg.command == "auth-logout"){
      console.log("Logging out");
      firebase.auth().signOut().then(function () {
        //user logged out...
        chrome.storage.local.set({ authInfo: false });
        resp({type: "result", status: "success", data: false});
      },function (error) {
        //logout error....
        resp({type: "result", status: "error", data: false,message: error});
      });
    }


    if(msg.command == "auth-login"){
      console.log("Logging the user out");



      //Login via Google SSO
      chrome.identity.getAuthToken({interactive: true}, function(token) {
        if (chrome.runtime.lastError) {
          console.log('It was not ayush possible to get a token programmatically.');

        } else if (token) {
          console.log("Received Token Bruh");

          var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
          firebase.auth().signInWithCredential(credential).catch(function(error) {
            console.log("Sending Response");
            resp({type: "result", status: "error", data: false});

          });
        } else {
          console.error('The OAuth Token was null');
        }
      });

      firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
          chrome.storage.local.set({ authInfo: user });
          console.log("Boo Yeah baby");
          resp({type: "result", status: "success", data: user, userObj: user});
        } else {
          console.log("Boo No Baby");
          // No user is signed in.
          chrome.storage.local.set({ authInfo: false });
          resp({type: "result", status: "error", data: false});
        }
      });
    }
  });



} catch (e) {
  console.log(e);
}

async function logIn() {
  chrome.identity.getAuthToken({interactive: true}, function(token) {
    if (chrome.runtime.lastError) {
      throw('It was not ayush possible to get a token programmatically.');

    } else if (token) {
      console.log("Received Token Bruh");

      var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
      firebase.auth().signInWithCredential(credential).catch(function(error) {
        console.log("Sending Response");
        return({type: "result", status: "error", data: false});

      });
    } else {
      throw ("'The OAuth Token was null'");
      console.error('The OAuth Token was null');
    }
  });

  firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      chrome.storage.local.set({ authInfo: user });
      console.log("Boo Yeah baby");
      resp({type: "result", status: "success", data: user, userObj: user});
    } else {
      console.log("Boo No Baby");
      // No user is signed in.
      chrome.storage.local.set({ authInfo: false });
      resp({type: "result", status: "error", data: false});
    }
  });



}
Editor is loading...