title: Implement the JWT middleware in golang using gin package and secure the other APIs?
body: <p>I want to secure my APIs that an authorized user can access my APIs. Here the router is given below:- </p>
  
  <p>Router.go</p>
  
  <pre><code>//here the customer will register.
  Route{"SaveUser", "POST", "/signup", controller.SaveUser},
  //here the customer will login with its username and password.
  Route{"LoginUser", "POST", "/login", controller.Login},
  
  //APIs that a valid user can access
  Route{"SaveCustomers", "POST", "/customer", controller.SaveCustomers},
  Route{"GetCustomers", "GET", "/customer", controller.GetCustomers},
  Route{"GetCustomer", "GET", "/customer/:id", controller.GetCustomer},
  Route{"UpdateCustomers", "PUT", "/customer/:id", controller.UpdateCustomers},
  </code></pre>
  
  <p>Controller.Login.go</p>
  
  <pre><code>package controller
  import(
    // "encoding/json"
    "github.com/gin-gonic/gin"
    "go-training/Template1/config"
    "go-training/Template1/models"
    "fmt"
    "github.com/dgrijalva/jwt-go"
    "gopkg.in/mgo.v2/bson"
    "strings"
   )
  
   type User struct {
     Email string `json:"email"`
     Password  string    `json:"password"`
     jwt.StandardClaims
   }
  
   type user struct {
     email     string 
     password string 
   }
   func Login(c *gin.Context) {
    email := c.PostForm("email")
  password := c.PostForm("password")
  reqBody := new(user)
  err := c.Bind(reqBody)
  if err != nil {
      fmt.Println(err)
  }
  response := ResponseControllerList{}
  conditions := bson.M{"email_id":email,"password":password}
  data, err := models.GetAllUser(conditions)
  dataCount, err := models.GetRecordsCount(config.SignupCollection, conditions)
  counter:= 0
  for _, signup := range data {
      if email == signup.EmailId &amp;&amp; password == signup.Password {
          //fmt.Println("heloo")
          counter = 1
      }
  }
  if counter == 1 {
      fmt.Println("Match!")
      token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), &amp;User{
          Email: email,
          Password:  password,
      })
      fmt.Println(token)
      tokenstring, err := token.SignedString([]byte(""))
      if err != nil {
          fmt.Println(err)
      }
      fmt.Println(tokenstring)
   }
  
  }
  </code></pre>
  
  <p>The above code will generate the token for user then how would I make a middleware that compare each given token in postman header with the database save token and </p>
  
  <blockquote>
    <p><strong>If</strong> the authorized user will come then it will access the above mentioned APIs. <strong>If</strong> it is not a authorized user then it will not access the above APIs and gives the error of <code>unauthorized user</code>.</p>
  </blockquote>
  
  <p>I don't understand what should I have to write for this. I googled it so many times and find the stuff like <a href="https://github.com/appleboy/gin-jwt" rel="nofollow noreferrer">this</a> but I don't understand the code in easy way. Can any one tell me what shall I do to secure the APIs. Thank you in advance. </p>