我正在嘗試將用戶資訊從登錄匯出到其他模塊。
export function login(){
console.log("entrando A LOGIN")
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth()
.signInWithPopup(provider)
.then((result) => {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
var token = credential.accessToken;
// The signed-in user info.
let user = result.user;
module.exports.user=user /// it says it does not provide user variable when in this ine I am
doing it
}等等....
/// 它說它不提供用戶變數,但我提供了。謝謝我是新手
uj5u.com熱心網友回復:
module.exports據我所知,你根本不需要使用。而且您真的不想將它們與 es6 模塊混合使用。
// google-auth.js
export function login() {
const provider = new firebase.auth.GoogleAuthProvider();
return firebase.auth()
.signInWithPopup(provider)
.then((result) => {
const credential = result.credential;
const token = credential.accessToken;
const user = result.user;
return user;
})
.catch(error => {
// you deal with errors that happen here
})
}
您可以通過從函式中回傳來獲取用戶物件。盡管如此,該函式回傳一個承諾,如果您嘗試執行類似要將函式匯入另一個檔案并以可以訪問用戶物件的方式使用它,您可以執行以下操作:const user = login()它不會作業的操作
// login.js
import { login } from './path/to/google-auth.js'
const authInstance = login();
來自官方檔案:
import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
// ...
}).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/333947.html
標籤:javascript 验证 异步 模块
上一篇:在寫".2f"或類似的東西時,我們能否在printf中得到下劃線而不是空白處?
下一篇:默認引數與多載?
