title: How do I use a Firebase refresh token to persist authentication?
body: <p>I have been trying to figure this out for weeks and either can't seem to understand the documentation, or something. I appreciate any help you can give.</p>
  <p>I am using the Firebase SDK</p>
  <p>I have my server-side route, in which I can access the token and could send it to the front:</p>
  <pre><code>const admin = require(&quot;firebase-admin&quot;)
  admin.initializeApp()
  
  exports.loginRoute = (req, res) =&gt; {
      const user = {
          email: req.body.email,
          password: req.body.password
      }
      const { valid, errors } = validateLoginData(user)
  
      if (!valid) {
          return res.status(400).json(errors)
      }
  
      admin
          .auth()
          .signInWithEmailAndPassword(user.email, user.password)
          .then((data) =&gt; {
              console.log(data.user.refreshToken, &quot;refresh token&quot;)
              return data.user.getIdToken(true)
          })
          .then((token) =&gt; {
              return res.json({ token })
          })
          .catch((err) =&gt; {
              console.error(err)
              if (err.code === &quot;auth/user-not-found&quot;) {
                  return res.status(400).json({ general: &quot;User not found&quot; })
              } else if (err.code === &quot;auth/wrong-password&quot;) {
                  return res
                      .status(400)
                      .json({ password: &quot;User credentials don't match&quot; })
              } else {
                  res.status(500).json({
                      error: &quot;Something went wrong, please try again.&quot;
                  })
              }
          })
  }
  </code></pre>
  <p>Here is where I could use the refresh token (on the front end) to fetch a new authentication token, but I can't figure out how to create a route to do this:</p>
  <pre><code>if (token) {
          const decodedToken = jwtDecode(token)
          if (decodedToken.exp * 1000 &lt; Date.now()) {
              localStorage.setItem(&quot;Authentication&quot;, false)
      //axios request to persist authentication would go here
          }
      }
  </code></pre>
  <p>Does anyone have a route that would work, or advice on what to do?</p>
  <p>EDIT</p>
  <pre><code>const login = async (credentials) =&gt; {
          let token
          await axios
              .post(&quot;/api/login&quot;, credentials)
              .then((res) =&gt; {
                  token = res.data.token
                  const FBIdToken = `Bearer ${token}`
                  localStorage.setItem(&quot;token&quot;, token)
                  localStorage.setItem(&quot;FBIdToken&quot;, FBIdToken)
                  localStorage.setItem(&quot;Authentication&quot;, true)
                  context.setAuthenticated((prev) =&gt; true)
              })
              .then(() =&gt; {
                  context.getUserData()
              })
              .then(() =&gt; {
                  context.setUserState((prevUserState) =&gt; ({
                      ...prevUserState,
                      token
                  }))
              })
              .catch((err) =&gt; {
                  context.setUserErrors((prev) =&gt; ({
                      ...prev,
                      errors: err.response.data
                  }))
              })
          history.push(&quot;/&quot;)
      }
  </code></pre>
  <p>Observer (client-side):</p>
  <pre><code>firebase.auth().onAuthStateChanged((user) =&gt; {
          if (user) {
              firebase
                  .auth()
                  .currentUser.getIdToken(/* forceRefresh */ true)
                  .then((idToken) =&gt; {
                      const FBIdToken = `Bearer ${idToken}`
                      localStorage.setItem(&quot;FBIdToken&quot;, FBIdToken)
                  })
                  .catch((err) =&gt; {
                      console.log(err)
                  })
          } else {
              localStorage.removeItem(&quot;FBIdToken&quot;)
          }
      })
  </code></pre>