admin管理员组

文章数量:1202959

I am dealing with firebase auth now and I was following this Firebase document.

Visit

// When the user signs in with email and password.
firebase.auth().signInWithEmailAndPassword('[email protected]', '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);
  });
})

I expected that I could get a token by using that function. But It doesn't work because user in the code doesn't have getIdToken() function.

I am dealing with firebase auth now and I was following this Firebase document.

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

// When the user signs in with email and password.
firebase.auth().signInWithEmailAndPassword('[email protected]', '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);
  });
})

I expected that I could get a token by using that function. But It doesn't work because user in the code doesn't have getIdToken() function.

Share Improve this question asked Aug 18, 2019 at 9:19 Seungsik ShinSeungsik Shin 2051 gold badge3 silver badges6 bronze badges 4
  • Maybe it is because of space in arrow function = > – Lazar Nikolic Commented Aug 18, 2019 at 9:21
  • what's the firebase/auth package version you are using? – Federkun Commented Aug 18, 2019 at 9:31
  • @Federkun It is 7.22 – Seungsik Shin Commented Aug 18, 2019 at 11:25
  • @LazarNikolic I fixed that part, but I got same error still.. – Seungsik Shin Commented Aug 18, 2019 at 11:25
Add a comment  | 

2 Answers 2

Reset to default 11

Seems like things changed since 7.* version. To get it:

firebase.auth().signInWithEmailAndPassword('[email protected]', '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);
  });
})

Note, that you need to use user.user.getIdToken() now, or just use destructuring as I did in the example.

To get the id token, just call auth's currentUser#getIdToken directly.

const idToken = await firebase.auth().currentUser.getIdToken()

本文标签: