Firebase onAuthChanged returns 当前用户的 uid 但 getIdToken() returns 为空?
Firebase onAuthChanged returns the uid of the currently user but getIdToken() returns null?
[我正在使用 next.js 实现此实现]
我有这个认证提供商功能:
const AuthContext = createContext({});
export function AuthProvider({ children }) {
const [userCred, setUserCred] = useState()
useEffect(() => {
fetch('/api/userAuth').then(results => results.json()).then(data => setUserCred(data))
}, [userCred]);
return (
<AuthContext.Provider value={{ userCred }}>{children}</AuthContext.Provider>
);
}
export const useAuth = () =>useContext(AuthContext)
我已将提供的内容打包到我的主 _app 中,并且运行良好。唯一的问题是 getTokenId() 总是 returns 空。
//in main app
import "../styles/globals.css";
import { AuthProvider } from "../context/AuthProvider";
function MyApp({ Component, pageProps }) {
return (
<AuthProvider>
<Component {...pageProps} />
</AuthProvider>
);
}
export default MyApp;
但是,当我在登录功能中立即 console.log 来自 userCredential 的令牌时,令牌不是空的。
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => {
// Signed in
const user = userCredential.user;
token = userCredential._tokenResponse.idToken res.status(200).json({userId: user, token: token }); }) .catch((error) => {
const errorCode = error.code;
const errorMessage = error.message; res.status(401).json({errorCode: errorCode })
});
正如我在 firebase 用户凭据中看到的,有两种获取令牌的方法
getIdTokenResult(
forceRefresh?: boolean
): Promise<firebase.auth.IdTokenResult>;
/**
* Returns a JSON Web Token (JWT) used to identify the user to a Firebase
* service.
*
* Returns the current token if it has not expired. Otherwise, this will
* refresh the token and return a new one.
*
* @param forceRefresh Force refresh regardless of token
* expiration.
*/
getIdToken(forceRefresh?: boolean): Promise<string>;
所以你可以使用这些方法,例如。
const token = userCredential.getIdToken();
或
const token = userCredential.getIdTokenResult();
此外,它总是 returns 承诺,因此请确保您接受承诺的结果。谢谢 )
[我正在使用 next.js 实现此实现]
我有这个认证提供商功能:
const AuthContext = createContext({});
export function AuthProvider({ children }) {
const [userCred, setUserCred] = useState()
useEffect(() => {
fetch('/api/userAuth').then(results => results.json()).then(data => setUserCred(data))
}, [userCred]);
return (
<AuthContext.Provider value={{ userCred }}>{children}</AuthContext.Provider>
);
}
export const useAuth = () =>useContext(AuthContext)
我已将提供的内容打包到我的主 _app 中,并且运行良好。唯一的问题是 getTokenId() 总是 returns 空。
//in main app
import "../styles/globals.css";
import { AuthProvider } from "../context/AuthProvider";
function MyApp({ Component, pageProps }) {
return (
<AuthProvider>
<Component {...pageProps} />
</AuthProvider>
);
}
export default MyApp;
但是,当我在登录功能中立即 console.log 来自 userCredential 的令牌时,令牌不是空的。
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password) .then((userCredential) => {
// Signed in
const user = userCredential.user;
token = userCredential._tokenResponse.idToken res.status(200).json({userId: user, token: token }); }) .catch((error) => {
const errorCode = error.code;
const errorMessage = error.message; res.status(401).json({errorCode: errorCode })
});
正如我在 firebase 用户凭据中看到的,有两种获取令牌的方法
getIdTokenResult(
forceRefresh?: boolean
): Promise<firebase.auth.IdTokenResult>;
/**
* Returns a JSON Web Token (JWT) used to identify the user to a Firebase
* service.
*
* Returns the current token if it has not expired. Otherwise, this will
* refresh the token and return a new one.
*
* @param forceRefresh Force refresh regardless of token
* expiration.
*/
getIdToken(forceRefresh?: boolean): Promise<string>;
所以你可以使用这些方法,例如。
const token = userCredential.getIdToken();
或
const token = userCredential.getIdTokenResult();
此外,它总是 returns 承诺,因此请确保您接受承诺的结果。谢谢 )