當前位置: 首頁>>代碼示例>>Golang>>正文


Golang wtforms.NewPasswordField函數代碼示例

本文整理匯總了Golang中github.com/jimmykuu/wtforms.NewPasswordField函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPasswordField函數的具體用法?Golang NewPasswordField怎麽用?Golang NewPasswordField使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewPasswordField函數的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: changePasswordHandler

// URL: /change_password
// 修改密碼
func changePasswordHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)

	if !ok {
		http.Redirect(w, r, "/signin?next=/change_password", http.StatusFound)
		return
	}

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if r.Method == "POST" && form.Validate(r) {
		if form.Value("new_password") == form.Value("confirm_password") {
			currentPassword := encryptPassword(form.Value("current_password"))
			if currentPassword == user.Password {
				c := DB.C("users")
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"password": encryptPassword(form.Value("new_password"))}})
				message(w, r, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	renderTemplate(w, r, "account/change_password.html", map[string]interface{}{"form": form})
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:34,代碼來源:account.go

示例2: changePasswordHandler

// URL: /change_password
// 修改密碼
func changePasswordHandler(handler Handler) {
	user, _ := currentUser(handler)

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		if form.Value("new_password") == form.Value("confirm_password") {
			currentPassword := encryptPassword(form.Value("current_password"))
			if currentPassword == user.Password {
				c := handler.DB.C(USERS)
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"password": encryptPassword(form.Value("new_password"))}})
				message(handler, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	renderTemplate(handler, "account/change_password.html", BASE, map[string]interface{}{"form": form})
}
開發者ID:hello-kukoo,項目名稱:gopher,代碼行數:29,代碼來源:account.go

示例3: changePasswordHandler

// URL: /user_center/change_password
// 修改密碼
func changePasswordHandler(handler *Handler) {
	user, _ := currentUser(handler)

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		if form.Value("new_password") == form.Value("confirm_password") {
			if user.CheckPassword(form.Value("current_password")) {
				c := handler.DB.C(USERS)
				salt := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{
					"password": encryptPassword(form.Value("new_password"), salt),
					"salt":     salt,
				}})
				message(handler, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	handler.renderTemplate("user_center/change_password.html", BASE, map[string]interface{}{
		"form":   form,
		"active": "change_password",
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:35,代碼來源:user_center.go

示例4: loginHandler

// URL: /login
// 處理用戶登錄,如果登錄成功,設置Cookie
func loginHandler(handler Handler) {

	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", "", &wtforms.Required{}),
		wtforms.NewPasswordField("password", "密碼", &wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {

			if form.Value("username") == "sll" &&
				form.Value("password") == "123456" {
			} else {
				form.AddError("password", "密碼和用戶名不匹配")

				renderHtml(handler, "login.html", map[string]interface{}{"form": form})
				return
			}

			session, _ := store.Get(handler.Request, "user")
			session.Values["username"] = form.Value("username")
			session.Save(handler.Request, handler.ResponseWriter)

			http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)

			return
		}
	}

	renderHtml(handler, "login.html", map[string]interface{}{"form": form})
}
開發者ID:mysll,項目名稱:flynet,代碼行數:33,代碼來源:admin.go

示例5: wrapAuthHandler

// wrapAuthHandler返回符合 go.auth包要求簽名的函數.
func wrapAuthHandler(handler *Handler) func(w http.ResponseWriter, r *http.Request, u auth.User) {
	return func(w http.ResponseWriter, r *http.Request, u auth.User) {
		c := handler.DB.C(USERS)
		user := User{}
		session, _ := store.Get(r, "user")
		c.Find(bson.M{"username": u.Id()}).One(&user)
		//關聯github帳號,直接登錄
		if user.Provider == GITHUB_COM {
			session.Values["username"] = user.Username
			session.Save(r, w)
			http.Redirect(w, r, "/", http.StatusSeeOther)
		}
		form := wtforms.NewForm(wtforms.NewTextField("username", "用戶名", "", wtforms.Required{}),
			wtforms.NewPasswordField("password", "密碼", wtforms.Required{}))
		session.Values[GITHUB_EMAIL] = u.Email()
		session.Values[GITHUB_ID] = u.Id()
		session.Values[GITHUB_LINK] = u.Link()
		session.Values[GITHUB_NAME] = u.Name()
		session.Values[GITHUB_ORG] = u.Org()
		session.Values[GITHUB_PICTURE] = u.Picture()
		session.Values[GITHUB_PROVIDER] = u.Provider()
		session.Save(r, w)

		//關聯已有帳號
		if handler.Request.Method == "POST" {
			if form.Validate(handler.Request) {
				user := User{}
				err := c.Find(bson.M{"username": form.Value("username")}).One(&user)
				if err != nil {
					form.AddError("username", "該用戶不存在")
					handler.renderTemplate("accoun/auth_login.html", BASE, map[string]interface{}{"form": form})
					return
				}

				if !ComparePwd(form.Value("password"), user.Password) {
					form.AddError("password", "密碼和用戶名不匹配")

					handler.renderTemplate("account/auth_login.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
					return
				}
				c.UpdateId(user.Id_, bson.M{"$set": bson.M{
					"emal":       session.Values[GITHUB_EMAIL],
					"accountref": session.Values[GITHUB_NAME],
					"username":   session.Values[GITHUB_ID],
					"idref":      session.Values[GITHUB_ID],
					"linkref":    session.Values[GITHUB_LINK],
					"orgref":     session.Values[GITHUB_ORG],
					"pictureref": session.Values[GITHUB_PICTURE],
					"provider":   session.Values[GITHUB_PROVIDER],
				}})
				deleteGithubValues(session)
				session.Values["username"] = u.Name()
				session.Save(r, w)
				http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)
			}
		}
		handler.renderTemplate("account/auth_login.html", BASE, map[string]interface{}{"form": form})
	}
}
開發者ID:nosqldb,項目名稱:G,代碼行數:60,代碼來源:account.go

示例6: resetPasswordHandler

// URL: /reset/{code}
// 用戶點擊郵件中的鏈接,根據code找到對應的用戶,設置新密碼,修改完成後清除code
func resetPasswordHandler(handler *Handler) {
	vars := mux.Vars(handler.Request)
	code := vars["code"]

	var user User
	c := handler.DB.C(USERS)
	err := c.Find(bson.M{"resetcode": code}).One(&user)

	if err != nil {
		message(handler, "重設密碼", `無效的重設密碼標記,可能你已經重新設置過了或者鏈接已經失效,請通過<a href="/forgot_password">忘記密碼</a>進行重設密碼`, "error")
		return
	}

	form := wtforms.NewForm(
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "確認新密碼", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		if form.Value("new_password") == form.Value("confirm_password") {
			salt := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			c.Update(
				bson.M{"_id": user.Id_},
				bson.M{
					"$set": bson.M{
						"password":  encryptPassword(form.Value("new_password"), salt),
						"salt":      salt,
						"resetcode": "",
					},
				},
			)
			message(handler, "重設密碼成功", `密碼重設成功,你現在可以 <a href="/signin" class="btn btn-primary">登錄</a> 了`, "success")
			return
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	handler.renderTemplate("account/reset_password.html", BASE, map[string]interface{}{"form": form, "code": code, "account": user.Username})
}
開發者ID:zhaoshiling1017,項目名稱:gopher,代碼行數:42,代碼來源:account.go

示例7: signinHandler

// URL: /signin
// 處理用戶登錄,如果登錄成功,設置Cookie
func signinHandler(w http.ResponseWriter, r *http.Request) {
	next := r.FormValue("next")

	form := wtforms.NewForm(
		wtforms.NewHiddenField("next", next),
		wtforms.NewTextField("username", "用戶名", "", &wtforms.Required{}),
		wtforms.NewPasswordField("password", "密碼", &wtforms.Required{}),
	)

	if r.Method == "POST" {
		if form.Validate(r) {
			c := DB.C("users")
			user := User{}

			err := c.Find(bson.M{"username": form.Value("username")}).One(&user)

			if err != nil {
				form.AddError("username", "該用戶不存在")

				renderTemplate(w, r, "account/signin.html", map[string]interface{}{"form": form})
				return
			}

			if !user.IsActive {
				form.AddError("username", "郵箱沒有經過驗證,如果沒有收到郵件,請聯係管理員")
				renderTemplate(w, r, "account/signin.html", map[string]interface{}{"form": form})
				return
			}

			if user.Password != encryptPassword(form.Value("password")) {
				form.AddError("password", "密碼和用戶名不匹配")

				renderTemplate(w, r, "account/signin.html", map[string]interface{}{"form": form})
				return
			}

			session, _ := store.Get(r, "user")
			session.Values["username"] = user.Username
			session.Save(r, w)

			if form.Value("next") == "" {
				http.Redirect(w, r, "/", http.StatusFound)
			} else {
				http.Redirect(w, r, next, http.StatusFound)
			}

			return
		}
	}

	renderTemplate(w, r, "account/signin.html", map[string]interface{}{"form": form})
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:54,代碼來源:account.go

示例8: resetPasswordHandler

// URL: /reset/{code}
// 用戶點擊郵件中的鏈接,根據code找到對應的用戶,設置新密碼,修改完成後清除code
func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	code := vars["code"]

	var user User
	c := DB.C("users")
	err := c.Find(bson.M{"resetcode": code}).One(&user)

	if err != nil {
		message(w, r, "重設密碼", `無效的重設密碼標記,可能你已經重新設置過了或者鏈接已經失效,請通過<a href="/forgot_password">忘記密碼</a>進行重設密碼`, "error")
		return
	}

	form := wtforms.NewForm(
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "確認新密碼", wtforms.Required{}),
	)

	if r.Method == "POST" && form.Validate(r) {
		if form.Value("new_password") == form.Value("confirm_password") {
			c.Update(
				bson.M{"_id": user.Id_},
				bson.M{
					"$set": bson.M{
						"password":  encryptPassword(form.Value("new_password")),
						"resetcode": "",
					},
				},
			)
			message(w, r, "重設密碼成功", `密碼重設成功,你現在可以 <a href="/signin" class="btn btn-primary">登錄</a> 了`, "success")
			return
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	renderTemplate(w, r, "account/reset_password.html", map[string]interface{}{"form": form, "code": code, "account": user.Username})
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:40,代碼來源:account.go

示例9: signupHandler

// URL: /signup
// 處理用戶注冊,要求輸入用戶名,密碼和郵箱
func signupHandler(w http.ResponseWriter, r *http.Request) {
	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", "", wtforms.Required{}, wtforms.Regexp{Expr: `^[a-zA-Z0-9_]{3,16}$`, Message: "請使用a-z, A-Z, 0-9以及下劃線, 長度3-16之間"}),
		wtforms.NewPasswordField("password", "密碼", wtforms.Required{}),
		wtforms.NewTextField("email", "電子郵件", "", wtforms.Required{}, wtforms.Email{}),
	)

	if r.Method == "POST" {
		if form.Validate(r) {
			c := DB.C("users")

			result := User{}

			// 檢查用戶名
			err := c.Find(bson.M{"username": form.Value("username")}).One(&result)
			if err == nil {
				form.AddError("username", "該用戶名已經被注冊")

				renderTemplate(w, r, "account/signup.html", map[string]interface{}{"form": form})
				return
			}

			// 檢查郵箱
			err = c.Find(bson.M{"email": form.Value("email")}).One(&result)

			if err == nil {
				form.AddError("email", "電子郵件地址已經被注冊")

				renderTemplate(w, r, "account/signup.html", map[string]interface{}{"form": form})
				return
			}

			c2 := DB.C("status")
			var status Status
			c2.Find(nil).One(&status)

			id := bson.NewObjectId()
			username := form.Value("username")
			validateCode := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			index := status.UserIndex + 1
			err = c.Insert(&User{
				Id_:          id,
				Username:     username,
				Password:     encryptPassword(form.Value("password")),
				Email:        form.Value("email"),
				ValidateCode: validateCode,
				IsActive:     true,
				JoinedAt:     time.Now(),
				Index:        index,
			})

			if err != nil {
				panic(err)
			}

			c2.Update(bson.M{"_id": status.Id_}, bson.M{"$inc": bson.M{"userindex": 1, "usercount": 1}})

			// 發送郵件
			/*
							subject := "歡迎加入Golang 中國"
							message2 := `歡迎加入Golang 中國。請訪問下麵地址激活你的帳戶。

				<a href="%s/activate/%s">%s/activate/%s</a>

				如果你沒有注冊,請忽略這封郵件。

				©2012 Golang 中國`
							message2 = fmt.Sprintf(message2, config["host"], validateCode, config["host"], validateCode)
							sendMail(subject, message2, []string{form.Value("email")})

							message(w, r, "注冊成功", "請查看你的郵箱進行驗證,如果收件箱沒有,請查看垃圾郵件,如果還沒有,請給[email protected]發郵件,告知你的用戶名。", "success")
			*/
			// 注冊成功後設成登錄狀態
			session, _ := store.Get(r, "user")
			session.Values["username"] = username
			session.Save(r, w)

			// 跳到修改用戶信息頁麵
			http.Redirect(w, r, "/profile", http.StatusFound)
			return
		}
	}

	renderTemplate(w, r, "account/signup.html", map[string]interface{}{"form": form})
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:87,代碼來源:account.go

示例10: signinHandler

// URL: /signin
// 處理用戶登錄,如果登錄成功,設置Cookie
func signinHandler(handler *Handler) {
	// 如果已經登錄了,跳轉到首頁
	_, has := currentUser(handler)
	if has {
		handler.Redirect("/")
	}

	next := handler.Request.FormValue("next")

	form := wtforms.NewForm(
		wtforms.NewHiddenField("next", next),
		wtforms.NewTextField("username", "用戶名", "", &wtforms.Required{}),
		wtforms.NewPasswordField("password", "密碼", &wtforms.Required{}),
		wtforms.NewTextField("geetest_challenge", "challenge", ""),
		wtforms.NewTextField("geetest_validate", "validate", ""),
		wtforms.NewTextField("geetest_seccode", "seccode", ""),
	)

	geeTest := geetest.NewGeeTest(Config.GtCaptchaId, Config.GtPrivateKey)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			// 檢查驗證碼
			if !geeTest.Validate(form.Value("geetest_challenge"), form.Value("geetest_validate"), form.Value("geetest_seccode")) {
				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{
					"form":       form,
					"captchaErr": true,
				})
				return
			}

			c := handler.DB.C(USERS)
			user := User{}

			err := c.Find(bson.M{"username": form.Value("username")}).One(&user)

			if err != nil {
				form.AddError("username", "該用戶不存在")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{
					"form":  form,
					"gtUrl": geeTest.EmbedURL(),
				})
				return
			}

			if !user.IsActive {
				form.AddError("username", "郵箱沒有經過驗證,如果沒有收到郵件,請聯係管理員")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{
					"form":  form,
					"gtUrl": geeTest.EmbedURL(),
				})
				return
			}

			if !user.CheckPassword(form.Value("password")) {
				form.AddError("password", "密碼和用戶名不匹配")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{
					"form":  form,
					"gtUrl": geeTest.EmbedURL(),
				})
				return
			}

			session, _ := store.Get(handler.Request, "user")
			session.Values["username"] = user.Username
			session.Save(handler.Request, handler.ResponseWriter)

			if form.Value("next") == "" {
				http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)
			} else {
				http.Redirect(handler.ResponseWriter, handler.Request, next, http.StatusFound)
			}

			return
		}
	}

	handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{
		"form":  form,
		"gtUrl": geeTest.EmbedURL(),
	})
}
開發者ID:zhaoshiling1017,項目名稱:gopher,代碼行數:87,代碼來源:account.go

示例11: signupHandler

// URL: /signup
// 處理用戶注冊,要求輸入用戶名,密碼和郵箱
func signupHandler(handler *Handler) {
	// 如果已經登錄了,跳轉到首頁
	_, has := currentUser(handler)
	if has {
		handler.Redirect("/")
	}

	geeTest := geetest.NewGeeTest(Config.GtCaptchaId, Config.GtPrivateKey)

	var username string
	var email string
	session, _ := store.Get(handler.Request, "user")
	if handler.Request.Method == "GET" {
		//如果是從新建關聯過來的就自動填充字段
		if session.Values[GITHUB_PROVIDER] == GITHUB_COM {
			username = session.Values[GITHUB_ID].(string)
			email = session.Values[GITHUB_EMAIL].(string)
		}
	}
	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", username, wtforms.Required{}, wtforms.Regexp{Expr: `^[a-zA-Z0-9_]{3,16}$`, Message: "請使用a-z, A-Z, 0-9以及下劃線, 長度3-16之間"}),
		wtforms.NewPasswordField("password", "密碼", wtforms.Required{}),
		wtforms.NewTextField("email", "電子郵件", email, wtforms.Required{}, wtforms.Email{}),
		wtforms.NewTextField("geetest_challenge", "challenge", ""),
		wtforms.NewTextField("geetest_validate", "validate", ""),
		wtforms.NewTextField("geetest_seccode", "seccode", ""),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			// 檢查驗證碼
			if !geeTest.Validate(form.Value("geetest_challenge"), form.Value("geetest_validate"), form.Value("geetest_seccode")) {
				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{
					"form":       form,
					"gtUrl":      geeTest.EmbedURL(),
					"captchaErr": true,
				})
				return
			}

			c := handler.DB.C(USERS)

			result := User{}

			// 檢查用戶名
			err := c.Find(bson.M{"username": form.Value("username")}).One(&result)
			if err == nil {
				form.AddError("username", "該用戶名已經被注冊")

				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{
					"form":  form,
					"gtUrl": geeTest.EmbedURL(),
				})
				return
			}

			// 檢查郵箱
			err = c.Find(bson.M{"email": form.Value("email")}).One(&result)

			if err == nil {
				form.AddError("email", "電子郵件地址已經被注冊")

				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{
					"form":  form,
					"gtUrl": geeTest.EmbedURL(),
				})
				return
			}

			c2 := handler.DB.C(STATUS)
			var status Status
			c2.Find(nil).One(&status)

			id := bson.NewObjectId()
			username := form.Value("username")
			validateCode := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			salt := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			index := status.UserIndex + 1
			u := &User{
				Id_:          id,
				Username:     username,
				Password:     encryptPassword(form.Value("password"), salt),
				Avatar:       "", // defaultAvatars[rand.Intn(len(defaultAvatars))],
				Salt:         salt,
				Email:        form.Value("email"),
				ValidateCode: validateCode,
				IsActive:     true,
				JoinedAt:     time.Now(),
				Index:        index,
			}
			if session.Values[GITHUB_PROVIDER] == GITHUB_COM {
				u.GetGithubValues(session)
				defer deleteGithubValues(session)
			}
			err = c.Insert(u)
			if err != nil {
				logger.Println(err)
				return
//.........這裏部分代碼省略.........
開發者ID:zhaoshiling1017,項目名稱:gopher,代碼行數:101,代碼來源:account.go

示例12: signinHandler

// URL: /signin
// 處理用戶登錄,如果登錄成功,設置Cookie
func signinHandler(handler *Handler) {
	// 如果已經登錄了,跳轉到首頁
	_, has := currentUser(handler)
	if has {
		handler.Redirect("/")
	}

	next := handler.Request.FormValue("next")

	form := wtforms.NewForm(
		wtforms.NewHiddenField("next", next),
		wtforms.NewTextField("username", "用戶名", "", &wtforms.Required{}),
		wtforms.NewPasswordField("password", "密碼", &wtforms.Required{}),
		wtforms.NewTextField("captcha", "驗證碼", "", wtforms.Required{}),
		wtforms.NewHiddenField("captchaId", ""),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			// 檢查驗證碼
			if !captcha.VerifyString(form.Value("captchaId"), form.Value("captcha")) {
				form.AddError("captcha", "驗證碼錯誤")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			c := handler.DB.C(USERS)
			user := User{}

			err := c.Find(bson.M{"username": form.Value("username")}).One(&user)

			if err != nil {
				form.AddError("username", "該用戶不存在")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			if !user.IsActive {
				form.AddError("username", "郵箱沒有經過驗證,如果沒有收到郵件,請聯係管理員")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			if user.Password != encryptPassword(form.Value("password"), user.Salt) {
				form.AddError("password", "密碼和用戶名不匹配")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			session, _ := store.Get(handler.Request, "user")
			session.Values["username"] = user.Username
			session.Save(handler.Request, handler.ResponseWriter)

			if form.Value("next") == "" {
				http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)
			} else {
				http.Redirect(handler.ResponseWriter, handler.Request, next, http.StatusFound)
			}

			return
		}
	}

	form.SetValue("captcha", "")
	handler.renderTemplate("account/signin.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
}
開發者ID:ego008,項目名稱:gopher,代碼行數:76,代碼來源:account.go

示例13: signupHandler

// URL: /signup
// 處理用戶注冊,要求輸入用戶名,密碼和郵箱
func signupHandler(handler *Handler) {
	// 如果已經登錄了,跳轉到首頁
	_, has := currentUser(handler)
	if has {
		handler.Redirect("/")
	}

	var username string
	var email string
	session, _ := store.Get(handler.Request, "user")
	if handler.Request.Method == "GET" {
		//如果是從新建關聯過來的就自動填充字段
		if session.Values[GITHUB_PROVIDER] == GITHUB_COM {
			username = session.Values[GITHUB_ID].(string)
			email = session.Values[GITHUB_EMAIL].(string)
		}
	}
	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", username, wtforms.Required{}, wtforms.Regexp{Expr: `^[a-zA-Z0-9_]{3,16}$`, Message: "請使用a-z, A-Z, 0-9以及下劃線, 長度3-16之間"}),
		wtforms.NewPasswordField("password", "密碼", wtforms.Required{}),
		wtforms.NewTextField("email", "電子郵件", email, wtforms.Required{}, wtforms.Email{}),
		wtforms.NewTextField("captcha", "驗證碼", "", wtforms.Required{}),
		wtforms.NewHiddenField("captchaId", ""),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			// 檢查驗證碼
			if !captcha.VerifyString(form.Value("captchaId"), form.Value("captcha")) {
				form.AddError("captcha", "驗證碼錯誤")
				fmt.Println("captcha")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			c := handler.DB.C(USERS)

			result := User{}

			// 檢查用戶名
			err := c.Find(bson.M{"username": form.Value("username")}).One(&result)
			if err == nil {
				form.AddError("username", "該用戶名已經被注冊")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			// 檢查郵箱
			err = c.Find(bson.M{"email": form.Value("email")}).One(&result)

			if err == nil {
				form.AddError("email", "電子郵件地址已經被注冊")
				form.SetValue("captcha", "")

				handler.renderTemplate("account/signup.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
				return
			}

			c2 := handler.DB.C(STATUS)
			var status Status
			c2.Find(nil).One(&status)

			id := bson.NewObjectId()
			username := form.Value("username")
			validateCode := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			salt := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
			index := status.UserIndex + 1
			u := &User{
				Id_:          id,
				Username:     username,
				Password:     encryptPassword(form.Value("password"), salt),
				Avatar:       "", // defaultAvatars[rand.Intn(len(defaultAvatars))],
				Salt:         salt,
				Email:        form.Value("email"),
				ValidateCode: validateCode,
				IsActive:     true,
				JoinedAt:     time.Now(),
				Index:        index,
			}
			if session.Values[GITHUB_PROVIDER] == GITHUB_COM {
				u.GetGithubValues(session)
				defer deleteGithubValues(session)
			}
			err = c.Insert(u)
			if err != nil {
				logger.Println(err)
				return
			}

			c2.Update(nil, bson.M{"$inc": bson.M{"userindex": 1, "usercount": 1}})

			// 重新生成users.json字符串
			generateUsersJson(handler.DB)

//.........這裏部分代碼省略.........
開發者ID:ego008,項目名稱:gopher,代碼行數:101,代碼來源:account.go


注:本文中的github.com/jimmykuu/wtforms.NewPasswordField函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。