Example #1
0
func (this *UserController) Login() {
	uid := this.GetSession("Uid")
	if uid != nil {
		this.Redirect("/", 302)
	}

	if this.Ctx.Input.Param("0") == "submit" {
		user := models.User{
			Username: this.GetString("username"),
			Password: this.GetString("password"),
		}
		// Handle the flash messages
		err := user.LoginVerify()
		if err != nil {
			flash := beego.NewFlash()
			flash.Error(err.Error())
			flash.Store(&this.Controller)
		}
		if user.Login() == true {
			this.SetSession("Uid", this.GetString("username"))
			user.GetUserInfo()
			this.SetSession("id", user.Uid)

			// store the user ID in the session
			this.Redirect("/", 302)
		}
		//If login failed, flash a relevent message
	}

	this.Data["title"] = "Login"

	this.Layout = "layout.tpl"
	this.TplNames = "user/login.tpl"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHead"] = ""
	this.LayoutSections["Sidebar"] = ""
	this.LayoutSections["ErrorHead"] = "errorHead.tpl"
}