// Get implemented login page. func (this *LoginRouter) Get() { this.Data["IsLoginPage"] = true this.TplNames = "auth/login.html" loginRedirect := strings.TrimSpace(this.GetString("to")) if utils.IsMatchHost(loginRedirect) == false { loginRedirect = "/" } // no need login if this.CheckLoginRedirect(false, loginRedirect) { return } if len(loginRedirect) > 0 { this.Ctx.SetCookie("login_to", loginRedirect, 0, "/") } form := models.LoginForm{} this.SetFormSets(&form) }
// Login implemented user login. func (this *LoginRouter) Login() { this.Data["IsLoginPage"] = true this.TplNames = "auth/login.html" // no need login if this.CheckLoginRedirect(false) { return } var user models.User var key string ajaxErrMsg := "auth.login_error_ajax" form := models.LoginForm{} // valid form and put errors to template context if this.ValidFormSets(&form) == false { if this.IsAjax() { goto ajaxError } return } key = "auth.login." + form.UserName + this.Ctx.Input.IP() if times, ok := utils.TimesReachedTest(key, utils.LoginMaxRetries); ok { if this.IsAjax() { ajaxErrMsg = "auth.login_error_times_reached" goto ajaxError } this.Data["ErrorReached"] = true } else if models.VerifyUser(&user, form.UserName, form.Password) { loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to")) if utils.IsMatchHost(loginRedirect) == false { loginRedirect = "/" } else { this.Ctx.SetCookie("login_to", "", -1, "/") } // login user models.LoginUser(&user, &this.Controller, form.Remember) this.setLangCookie(i18n.GetLangByIndex(user.Lang)) if this.IsAjax() { this.Data["json"] = map[string]interface{}{ "success": true, "message": this.Tr("auth.login_success_ajax"), "redirect": loginRedirect, } this.ServeJson() return } this.Redirect(loginRedirect, 302) return } else { utils.TimesReachedSet(key, times, utils.LoginFailedBlocks) if this.IsAjax() { goto ajaxError } } this.Data["Error"] = true return ajaxError: this.Data["json"] = map[string]interface{}{ "success": false, "message": this.Tr(ajaxErrMsg), "once": this.Data["once_token"], } this.ServeJson() }