(c *gin.Context)
| 25 | } |
| 26 | |
| 27 | func Login(c *gin.Context) { |
| 28 | if !common.PasswordLoginEnabled { |
| 29 | c.JSON(http.StatusOK, gin.H{ |
| 30 | "message": "管理员关闭了密码登录", |
| 31 | "success": false, |
| 32 | }) |
| 33 | return |
| 34 | } |
| 35 | var loginRequest LoginRequest |
| 36 | err := json.NewDecoder(c.Request.Body).Decode(&loginRequest) |
| 37 | if err != nil { |
| 38 | c.JSON(http.StatusOK, gin.H{ |
| 39 | "message": "无效的参数", |
| 40 | "success": false, |
| 41 | }) |
| 42 | return |
| 43 | } |
| 44 | username := loginRequest.Username |
| 45 | password := loginRequest.Password |
| 46 | if username == "" || password == "" { |
| 47 | c.JSON(http.StatusOK, gin.H{ |
| 48 | "message": "无效的参数", |
| 49 | "success": false, |
| 50 | }) |
| 51 | return |
| 52 | } |
| 53 | user := model.User{ |
| 54 | Username: username, |
| 55 | Password: password, |
| 56 | } |
| 57 | err = user.ValidateAndFill() |
| 58 | if err != nil { |
| 59 | c.JSON(http.StatusOK, gin.H{ |
| 60 | "message": err.Error(), |
| 61 | "success": false, |
| 62 | }) |
| 63 | return |
| 64 | } |
| 65 | setupLogin(&user, c) |
| 66 | } |
| 67 | |
| 68 | // setup session & cookies and then return user info |
| 69 | func setupLogin(user *model.User, c *gin.Context) { |
nothing calls this directly
no test coverage detected