Michael Nelles
Michael Nelles

Reputation: 6032

FireBase + React: TypeError: user.getToken is not a function

Here is the code signInWithEmailAndPassword works perfectly but when I try to acquire a token the error is always thrown TypeError: user.getToken is not a function.

import app from "./base.js";

app.auth()
   .signInWithEmailAndPassword(email, password)
   .then(function (user) {
      user.getToken().then(function (token) {
        localForage.setItem("token", token); // store the token
        console.log("pusing forward");
        history.push("/");
      });
    }).catch((error) => {
        console.log("err: " + error);
     });

base.js

import firebase from "firebase/app";
import "firebase/auth";

const app = firebase.initializeApp({
   apiKey: process.env.REACT_APP_FIREBASE_KEY,
   authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
   databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
   projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
   storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
   messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID,
});

export default app;

Comments welcome thanks.

Upvotes: 1

Views: 1646

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

According to the API documentation for signInWithEmailAndPassword() user is a UserCredential object. It doesn't have a getToken method. Perhaps you want to use its user property to get a User object, which has a getIdToken() method.

    user.user.getIdToken().then(token => { ... })

Here is a peak at the object user1 returned after calling signInWithEmailAndPassword(email, password) enter image description here

Upvotes: 1

Related Questions