Esempio n. 1
0
// Register implemented Post method for RegisterRouter.
func (this *RegisterRouter) Register() {
	this.Data["IsRegister"] = true
	this.TplNames = "auth/register.html"

	// no need login
	if this.CheckLoginRedirect(false) {
		return
	}

	form := models.RegisterForm{Locale: this.Locale}
	// valid form and put errors to template context
	if this.ValidFormSets(&form) == false {
		return
	}

	// Create new user.
	user := new(models.User)

	// set default Lang
	user.Lang = this.Locale.Index()

	if err := models.RegisterUser(user, form); err == nil {
		models.SendRegisterMail(this.Locale, user)

		// login user
		models.LoginUser(user, &this.Controller, false)

		this.FlashRedirect("/settings/profile", 302, "RegSuccess")

		return

	} else {
		beego.Error("Register: Failed ", err)
	}
}
Esempio n. 2
0
// 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()
}