// Login method func (c *LoginController) Login() { if c.Ctx.Request.Method == "GET" { c.renderLoginLayout() c.Data["Title"] = "登录" c.Data["ShowError"] = false c.TplName = "login.tpl" } else if c.Ctx.Request.Method == "POST" { var input struct { Username string `form:"username"` Password string `form:"password"` } c.ParseForm(&input) logger.Debug("Input:%+v", input) if input.Username == "" || input.Password == "" { c.RenderLoginError("用户名或密码不能为空", input.Username) return } user := models.FindUserByName(input.Username) if user != nil && user.CheckPassword(input.Password) { c.SetSession("uid", user.ID) uid := c.Ctx.Input.Session("uid") // sessionID := c.GetSession("uid") logger.Debug("sessionID: %v", uid) c.Redirect("/dashboard", 302) } else { c.RenderLoginError("用户名或密码错误", input.Username) } } }
// Create method func (c *UserController) Create() { var input struct { Username string `form:"username"` Password string `form:"password"` PasswordConfirm string `form:"password_confirm"` } if err := c.ParseForm(&input); err != nil { logger.Critical("Register error: %v", err) c.Abort("500") } if input.Username == "" || input.Password == "" || input.PasswordConfirm == "" { c.RenderCreateError("请填入完整的注册信息", input.Username) return } if input.Password != input.PasswordConfirm { c.RenderCreateError("两次输入的密码不一致", input.Username) return } if user := models.FindUserByName(input.Username); user != nil { c.RenderCreateError("用户名已被占用", input.Username) return } user := &models.User{Name: input.Username, Password: input.Password, PasswordChanged: true} if err := user.Save(); err != nil { logger.Critical("Register error: %v", err) c.Abort("500") } c.Redirect("/pending", 302) }