getIdToken() 不是函数,即使它像 firebase 文档中的函数一样使用

Seu*_*hin 6 javascript node.js firebase firebase-authentication

我现在正在处理 firebase auth,我正在关注这个 Firebase 文档。

访问https://firebase.google.com/docs/auth/admin/manage-cookies#sign_in

// When the user signs in with email and password.
firebase.auth().signInWithEmailAndPassword('user@example.com', 'password').then(user => {
  // Get the user's ID token as it is needed to exchange for a session cookie.
  return user.getIdToken().then(idToken = > {
    // Session login endpoint is queried and the session cookie is set.
    // CSRF protection should be taken into account.
    // ...
    const csrfToken = getCookie('csrfToken')
    return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken);
  });
})
Run Code Online (Sandbox Code Playgroud)

我希望我可以通过使用该功能获得令牌。但它不起作用,因为代码中的用户没有 getIdToken() 函数。

Foo*_*iko 8

似乎自7.*版本以来事情发生了变化。为拿到它,为实现它:

firebase.auth().signInWithEmailAndPassword('user@example.com', 'password').then(({ user }) => {
  // Get the user's ID token as it is needed to exchange for a session cookie.
  return user.getIdToken().then(idToken = > {
    // Session login endpoint is queried and the session cookie is set.
    // CSRF protection should be taken into account.
    // ...
    const csrfToken = getCookie('csrfToken')
    return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken);
  });
})
Run Code Online (Sandbox Code Playgroud)

请注意,您user.user.getIdToken()现在需要使用,或者像我在示例中所做的那样使用解构。


Fed*_*kun 6

要获取 id 令牌,只需currentUser#getIdToken直接调用 auth即可。

const idToken = await firebase.auth().currentUser.getIdToken()
Run Code Online (Sandbox Code Playgroud)