Пример #1
0
func wikiLogin(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		form := struct {
			Username   string
			Password   string
			ErrorLogin string
		}{
			"", "", "",
		}
		writeLoginForm(w, form)
	}
	if r.Method == "POST" {
		// Get form field values
		username := r.FormValue("username")
		password := r.FormValue("password")

		// Validate form fields
		user := models.UserByUsernameAndPassword(r, username, password)
		if len(user.Username) > 0 {
			if username == user.Username && password == user.Password {
				if wikiUserIdCookie == nil {
					wikiUserIdCookie = securecookie.New(wikiSecret, nil)
				}
				stringID := fmt.Sprintf("%d", user.Id)
				tools.StoreCookie(w, r, wikiUserIdCookie, "user_id", stringID)

				// set the current user
				currentUser = user

				// redirect to the wiki front page
				http.Redirect(w, r, "/wiki", http.StatusFound)
				return
			}
		}

		form := struct {
			Username   string
			Password   string
			ErrorLogin string
		}{
			username,
			password,
			"Invalid Login",
		}

		writeLoginForm(w, form)
	}
}
Пример #2
0
func unit4Signup(w http.ResponseWriter, r *http.Request) {

	if r.Method == "GET" {
		form := struct {
			Username      string
			Password      string
			Verify        string
			Email         string
			ErrorUsername string
			ErrorPassword string
			ErrorVerify   string
			ErrorEmail    string
		}{
			"", "", "", "", "", "", "", "",
		}
		writeForm(w, form)
	}
	if r.Method == "POST" {
		errorUsername := ""
		errorPassword := ""
		errorVerify := ""
		errorEmail := ""
		// Get form field values
		username := r.FormValue("username")
		password := r.FormValue("password")
		verify := r.FormValue("verify")
		email := r.FormValue("email")
		// Validate form fields
		if !(validUsername(username) && validPassword(password) && (password == verify) && validEmail(email)) {
			if !validUsername(username) {
				errorUsername = "******"
			}
			if !validPassword(password) {
				errorPassword = "******"
			}
			if password != verify {
				errorVerify = "Your passwords didn't match"
			}
			if !validEmail(email) {
				errorEmail = "That's not a valid email"
			}

			password = ""
			verify = ""

			form := struct {
				Username      string
				Password      string
				Verify        string
				Email         string
				ErrorUsername string
				ErrorPassword string
				ErrorVerify   string
				ErrorEmail    string
			}{
				username,
				password,
				verify,
				email,
				errorUsername,
				errorPassword,
				errorVerify,
				errorEmail,
			}

			writeForm(w, form)
		} else {
			user := models.UserByUsername(r, username)

			if len(user.Username) > 0 {
				errorUsername = "******"

				form := struct {
					Username      string
					Password      string
					Verify        string
					Email         string
					ErrorUsername string
					ErrorPassword string
					ErrorVerify   string
					ErrorEmail    string
				}{
					username,
					password,
					verify,
					email,
					errorUsername,
					errorPassword,
					errorVerify,
					errorEmail,
				}

				writeForm(w, form)
			} else {
				c := appengine.NewContext(r)

				userID, _, _ := datastore.AllocateIDs(c, "User", nil, 1)
				key := datastore.NewKey(c, "WikiUser", "", userID, nil)
				u := models.User{userID, username, password, verify, email, time.Now()}
				_, err := datastore.Put(c, key, &u)
				if err != nil {
					http.Error(w, err.Error(), http.StatusInternalServerError)
					return
				}

				userIdCookie = securecookie.New(secret, nil)

				stringID := fmt.Sprintf("%d", key.IntID())
				tools.StoreCookie(w, r, userIdCookie, "user_id", stringID)

				// redirect to the page of the newly registered user
				http.Redirect(w, r, "/unit4/welcome", http.StatusFound)
				return
			}
		}
	}
}