当前位置:   article > 正文

实现谷歌第三方登录步骤_googleoauthprovider文档

googleoauthprovider文档

前言

主要是通过Firebase实现谷歌登录

首先需要有一个谷歌账号登录到Firebase

文档地址https://firebase.google.com/docs/auth/web/google-signin?hl=zh-cn

按照文档上的步骤

第一步吧项目添加到Firebase中

添加成功之后会生成一段代码  放到自己项目的main.js文件中  这样就可以让项目和Firebase建立联系

  1. // Import the functions you need from the SDKs you need
  2. import { initializeApp } from "firebase/app";
  3. import { getAnalytics } from "firebase/analytics";
  4. // TODO: Add SDKs for Firebase products that you want to use
  5. // https://firebase.google.com/docs/web/setup#available-libraries
  6. // Your web app's Firebase configuration
  7. // For Firebase JS SDK v7.20.0 and later, measurementId is optional
  8. const firebaseConfig = {
  9. apiKey: "xxx",
  10. authDomain: "xxx",
  11. projectId: "xxx",
  12. storageBucket: "xxx",
  13. messagingSenderId: "xxx",
  14. appId: "xxx",
  15. measurementId: "xxx"
  16. };
  17. // Initialize Firebase
  18. const app = initializeApp(firebaseConfig);
  19. const analytics = getAnalytics(app);
  20. // Get a list of cities from your database
  21. async function getCities(db) {
  22. const citiesCol = collection(db, 'cities');
  23. const citySnapshot = await getDocs(citiesCol);
  24. const cityList = citySnapshot.docs.map(doc => doc.data());
  25. return cityList;
  26. }

第二步

在登录页面引入一下代码

  1. import { getAuth, signInWithPopup, GoogleAuthProvider, } from "firebase/auth";
  2. const auth = getAuth();
  3. const provider = new GoogleAuthProvider();
  4. //点击登录调用这个方法 就会弹出谷歌登录弹出
  5. signInWithPopup(auth, provider)
  6. .then((result) => {
  7. // This gives you a Google Access Token. You can use it to access the Google API.
  8. const credential = GoogleAuthProvider.credentialFromResult(result);
  9. //credential 这个里面会有一个idtoken 我们后端是用这个token获取用户信息
  10. const token = credential.accessToken;
  11. // The signed-in user info.
  12. const user = result.user;
  13. // IdP data available using getAdditionalUserInfo(result)
  14. // ...
  15. }).catch((error) => {
  16. // Handle Errors here.
  17. const errorCode = error.code;
  18. const errorMessage = error.message;
  19. // The email of the user's account used.
  20. const email = error.customData.email;
  21. // The AuthCredential type that was used.
  22. const credential = GoogleAuthProvider.credentialFromError(error);
  23. // ...
  24. });

以上代码文档里面都可以查到

 

注意 !!!  需要再文档里面给自己的网域添加授权才可以实现最终效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/706911
推荐阅读
相关标签
  

闽ICP备14008679号