func (mongo *Mongo) Login(c *gin.Context) { var Incoming struct { Username string `json:"username"` Password string `json:"password"` } err := c.BindJSON(&Incoming) if err != nil { println(err) c.JSON(http.StatusInternalServerError, gin.H{ "err": true, "msg": "Error parsing request.", }) return } var t models.Teacher t.Username = Incoming.Username t.Password = Incoming.Password println("Got from user", Incoming.Username, t.Username, t.Password) teacher := t.GetByUsernameAndPassword(mongo.Database) if teacher.Subject != "" { /** Create the token */ token := jwt.New(jwt.SigningMethodHS256) token.Claims["username"] = teacher.Username token.Claims["subject"] = teacher.Subject token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix() tokenString, err := token.SignedString([]byte(utils.MySigningKey)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "err": true, "msg": "Error occured while creating a token", }) return } c.JSON(http.StatusOK, gin.H{ "err": false, "msg": "Got", "teacher": teacher, "token": tokenString, }) } else { c.JSON(http.StatusInternalServerError, gin.H{ "err": true, "msg": "Not found.", }) } }