LoginAccount validates user account.
(c *gin.Context)
| 61 | |
| 62 | // LoginAccount validates user account. |
| 63 | func LoginAccount(c *gin.Context) { |
| 64 | var p accountPayload |
| 65 | if e := c.BindJSON(&p); e != nil { |
| 66 | util.BindFailFn(c, e) |
| 67 | return |
| 68 | } |
| 69 | if e := p.validate(); e != nil { |
| 70 | c.JSON(http.StatusOK, gin.H{ |
| 71 | "code": 1, |
| 72 | "data": e.Error(), |
| 73 | }) |
| 74 | return |
| 75 | } |
| 76 | // gen token |
| 77 | token := jwt.New(jwt.GetSigningMethod("HS256")) |
| 78 | token.Claims = jwt.MapClaims{ |
| 79 | "username": p.Username, |
| 80 | "exp": time.Now().Add(time.Hour * 1).Unix(), |
| 81 | } |
| 82 | |
| 83 | tokenString, err := token.SignedString([]byte(util.JWTSecret)) |
| 84 | if err != nil { |
| 85 | c.JSON(http.StatusInternalServerError, gin.H{ |
| 86 | "code": 0, |
| 87 | "data": "Could not generate token", |
| 88 | }) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | c.JSON(http.StatusOK, gin.H{ |
| 93 | "code": 0, |
| 94 | "data": map[string]string{ |
| 95 | "status": "ok", |
| 96 | "type": p.Type, |
| 97 | "currentAuthority": "admin", |
| 98 | "token": tokenString, |
| 99 | }, |
| 100 | }) |
| 101 | } |