title: Golang auth. Parsing JWT: illegal base64 data at input byte 0
body: <p>I am making an authentication form on my web app.</p>
  <p>First, I send the form data to server, it produces the token and I store it in the localStorage</p>
  <p>Then, I want to redirect from form page to the home page. Here is the JavaScript that sends the token to server so that I would be seen as an authorized user on the home page:</p>
  <pre><code>const xhr = new XMLHttpRequest()
  xhr.open('GET', '/')
  xhr.setRequestHeader('Authorization', localStorage.token)
  xhr.send()
  xhr.onload  = () =&gt; {
  if(xhr.status &gt;= 400) {
      console.log(&quot;error&quot;)
    }
  }
  
  xhr.onerror = () =&gt; {
      console.log(&quot;error&quot;)
  }
  </code></pre>
  <p>Then I want to check the token and show the home page. Here is the Golang func for it:</p>
  <pre><code>func (h *Handler) Home_page(c *gin.Context) {
  header := c.GetHeader(&quot;Authorization&quot;)
      if header != &quot;&quot; {
          _, err := h.services.Authorization.ParseToken(header)
          if err != nil {
              newErrorResponse(c, http.StatusUnauthorized, err.Error())
              return
          }
          c.HTML(
              http.StatusOK,
              &quot;home_page.gohtml&quot;,
              gin.H{
                  &quot;IsAuth&quot;: true,
              },
          )
          return
      }
  }
  </code></pre>
  <p>ParseToken func:</p>
  <pre><code>
  func (s *AuthService) ParseToken(accessToken string) (int, error) {
      token, err := jwt.ParseWithClaims(accessToken, &amp;tokenClaims{}, func(token *jwt.Token) (interface{}, error) {
          if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
              return nil, errors.New(&quot;invalid signing method&quot;)
          }
          return []byte(signingKey), nil //signingKey is a string with random elements
      })
      if err != nil {
          return 0, err
      }
  
      claims, ok := token.Claims.(*tokenClaims)
      if !ok {
          return 0, errors.New(&quot;token claims are not of type *tokenClaims&quot;)
      }
  
      return claims.UserId, nil
  }
  </code></pre>
  <p>The problem is that I get this error:</p>
  <p><em>ERRO[0001] illegal base64 data at input byte 0</em></p>
  <p>I already checked the token on the jwt.io, it shows that the token is verified, maybe the problem is in the type of string that I am passing as a token.</p>
  <p>Another problem is that if I don't check the error returning from token validation,  the home page doesn't show with this error:</p>
  <p><em>http: panic serving [::1]:50490: write tcp [::1]:8083-&gt;[::1]:50490: write: broken pipe</em></p>
  <br>
  I am new to Golang, was struggling with this problem, though it seems typical.
  <p>I would be very thankful for any help on how to process the token or to redirect from the authentication form!</p>
  <br>
  <p><strong>Edit:</strong> I am using the <a href="https://github.com/golang-jwt/jwt" rel="nofollow noreferrer">https://github.com/golang-jwt/jwt/v4</a></p>