Exemple #1
0
func loginHandler(c *util.Context) error {
	send := func(invalid bool) error {
		return c.T("l", "login").Execute(c.Writer, map[string]interface{}{
			"context":  c,
			"next":     c.Request.FormValue("next"),
			"username": c.Request.FormValue("username"),
			"invalid":  invalid,
		})
	}

	// Simple get
	if c.Request.Method == "GET" {
		fmt.Println("get")
		return send(false)
	}

	// Form validation pass
	form := loginForm(c.Request)
	if !form.IsValid() {
		fmt.Println("invalid")
		return send(true)
	}

	user, err := data.Login(c.Database,
		c.Request.FormValue("username"),
		c.Request.FormValue("password"))

	// Login failed -> bad creds
	if err != nil {
		fmt.Println(err)
		return send(true)
	}

	// Log him in!
	c.Session.Values["user"] = user.ID
	if c.Request.FormValue("next") != "" {
		c.Redirect(c.Request.FormValue("next"))
	} else {
		c.Redirect("/dashboard")
	}
	return nil
}
Exemple #2
0
func signupPostHandler(c *util.Context) error {
	// Setup stripe client
	stripeClient := &stripe.Client{}
	stripeClient.Init(util.Getenv("STRIPE_PRIVATE_KEY"), nil, nil)

	// Gater form values
	fullname := c.Request.FormValue("fullname")
	accountName := c.Request.FormValue("account")
	email := c.Request.FormValue("email")
	password := c.Request.FormValue("password")
	plan := c.Request.FormValue("plan")
	token := c.Request.FormValue("stripeToken")

	// Store new user in mongo
	user := data.User{
		Email:    email,
		Fullname: fullname,
	}
	user.SetPassword(password)
	err := user.Create(c.C("users"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Now create his account
	account := data.Account{
		Name: accountName,
		Plan: plan,
	}
	account.Users = []bson.ObjectId{user.ID}
	err = account.Create(c.C("accounts"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Create and Customer, subscribe him, start trial, associate card (1-step)
	customer := &stripe.CustomerParams{
		Token: token,
		Desc:  fullname,
		Email: email,
		Plan:  plan,
		Params: stripe.Params{
			Meta: map[string]string{
				"uid": user.ID.String(),
			},
		},
	}
	newCustomer, err := stripeClient.Customers.Create(customer)

	// Save new imformation (stripe id) and add account id to user accounts
	user.StripeId = newCustomer.Id
	user.Accounts = []bson.ObjectId{account.ID}
	c.C("users").UpdateId(user.ID, user)

	if err != nil {
		log.Print(err)
		return err
	}

	c.Redirect(c.RouteUrl("login") + "?next=" + url.QueryEscape(appUrl))
	return nil
}