示例#1
0
func SignupAction(env Env) (status Status, headers Headers, body Body) {

	user := users.NewUser()
	formdata.UnmarshalByNames(env.Request().Request, user,
		[]string{"Email", "Name", "Password", "ConfirmPassword"})

	if validated := user.ValidateSignupForm(); validated.HasError() {
		mangotemplate.ForRender(env, "sessions/signup", &SessionData{User: user, Validated: validated})
		return
	}

	if validated := user.ValidateEmailExist(); validated.HasError() {
		mangotemplate.ForRender(env, "sessions/signup", &SessionData{User: user, Validated: validated})
		return
	}

	if err := user.Signup(); err != nil {
		panic(err)
		return
	}

	services.PutUserIdToSession(env, user.Id.Hex())

	return Redirect(http.StatusFound, "/")
}
示例#2
0
func LoginAction(env Env) (status Status, headers Headers, body Body) {
	user := users.NewUser()
	formdata.UnmarshalByNames(env.Request().Request, user, []string{"Email", "Password"})

	validated := user.ValidateLoginForm()
	if validated.HasError() {
		mangotemplate.ForRender(env, "sessions/login", &SessionData{User: user, Validated: validated})
		return
	}

	loginUser := users.LoginWith(user.Email, user.Password)
	if loginUser == nil {
		validated.AddError("Password", "User and password do not match!")
		mangotemplate.ForRender(env, "sessions/login", &SessionData{User: user, Validated: validated})
		return
	}

	services.PutUserIdToSession(env, loginUser.Id.Hex())
	return Redirect(http.StatusFound, "/")
}