Esempio n. 1
0
// The user is registered and redirected to the landing page
func SignupHandler(response http.ResponseWriter, request *http.Request) {
	username := request.FormValue("username")
	password := request.FormValue("password")
	redirect_target := "/index"

	if username != "" && password != "" {
		user := models.NewUser()
		user.Fields = make(map[string]string)
		user.Fields["username"] = username
		user.Fields["password"] = password
		if _, err := account.Register(user); err == nil {
			account.Login(username, password, response)
			redirect_target = "/config"
		}
	}
	http.Redirect(response, request, redirect_target, http.StatusFound)
}
func getCurrentUser(r *http.Request, t *oauth.Transport) (*models.User, error) {
	// Make the request.
	request, err := t.Client().Get("https://www.googleapis.com/oauth2/v1/userinfo?alt=json")

	if err != nil {
		return nil, err
	}

	if userInfo, err := ioutil.ReadAll(request.Body); err == nil {
		var gu models.GoogleUser

		if err := json.Unmarshal(userInfo, &gu); err == nil {
			// create new user if he doesn't already exist
			if currentUser, err = models.GetUser(r, gu.Id, gu.Email); err != nil {
				currentUser, err = models.NewUser(r, gu.Id, gu.Email, gu.Name, "google")
			}

			return currentUser, err
		}
	}

	return nil, err
}