diff --git a/handlers.go b/handlers.go index a0cdd85..0748edd 100644 --- a/handlers.go +++ b/handlers.go @@ -60,14 +60,14 @@ func home(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) + err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) } case http.MethodPost: - err = text.Execute(w, templatedata{}) + err := text.Execute(w, templatedata{}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -85,7 +85,7 @@ func login(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - text.Execute(w, templatedata{}) + err := text.Execute(w, templatedata{}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -111,14 +111,27 @@ func login(w http.ResponseWriter, r *http.Request) { if len(errors) > 0 { - text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(r), FormErrors: errors}) + err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(r), FormErrors: errors}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) } + + return + } + + id, err := users.Authenticate(username, password) + if err == ErrInvalidCredentials { + errors["generic"] = "Email or Password is incorrect" + err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(r), FormErrors: errors}) + if err != nil { + log.Fatal(err) + http.Error(w, "Internal Server Error", 500) + } + + return } - id, _ := users.Authenticate(username, password) if id > 0 { session.Values["id"] = id fmt.Println("Logged in with id:", id) @@ -141,7 +154,7 @@ func logout(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) + err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -163,10 +176,13 @@ func register(w http.ResponseWriter, r *http.Request) { log.Fatal(err) } + id := authenticated_user(r) + account, err := users.Get_account(id) + switch r.Method { case http.MethodGet: - text.Execute(w, templatedata{}) + err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -191,7 +207,7 @@ func register(w http.ResponseWriter, r *http.Request) { if len(errors) > 0 { - text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(r), FormErrors: errors}) + err := text.Execute(w, templatedata{AuthenticatedUser: authenticated_user(r), FormErrors: errors}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -226,7 +242,7 @@ func account(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) + err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account}) if err != nil { log.Fatal(err) http.Error(w, "Internal Server Error", 500) @@ -240,3 +256,21 @@ func account(w http.ResponseWriter, r *http.Request) { // http.Error(w, "Internal Server Error", 500) // } } + +func deleteaccount(w http.ResponseWriter, r *http.Request) { + + id := authenticated_user(r) + + switch r.Method { + case http.MethodPost: + fmt.Println("Deleting account with id ", id) + users.Delete(id) + + session, _ := store.Get(r, "id"); + + session.Values["id"] = 0; + session.Save(r, w) + + http.Redirect(w, r, "/", http.StatusSeeOther) + } +} diff --git a/main.go b/main.go index 0f53651..23b0ea0 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import _ "github.com/lib/pq" import "database/sql" import "github.com/gorilla/sessions" import "regexp" -//import "golang.org/x/crypto/bcrypt" +import "golang.org/x/crypto/bcrypt" import "github.com/stripe/stripe-go/v78" import "github.com/stripe/stripe-go/v78/customer" @@ -54,6 +54,10 @@ func main() { flag.Parse() fmt.Println("Hello, Sailor!") + h, _ := bcrypt.GenerateFromPassword([]byte("password"), 12) + + fmt.Println(string(h)) + stripe.Key = "sk_test_51PGebgKUHKCjyTmc97rfDPcvew6EhqDz2qp3U7XoAMIilAU9IVo2NO4P7ylkTvbBafFVr94trha1VYY32jRWMw2K00Yq7YJXFf" c, _ := customer.Get("cus_Q7am78hLcLUvGQ", nil) @@ -106,7 +110,7 @@ func main() { mux.HandleFunc("/logout", logout) mux.HandleFunc("/register", register) mux.HandleFunc("/account", require_authenticated_user(account)) - + mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount)) log.Fatal(http.ListenAndServe(*addr, secure_headers(mux))) diff --git a/models.go b/models.go index 117f8df..953a678 100644 --- a/models.go +++ b/models.go @@ -9,11 +9,14 @@ import "time" import "golang.org/x/crypto/bcrypt" import "database/sql" import _ "github.com/lib/pq" -import _ "fmt" +import "fmt" -var Errnorecord = errors.New("no matching record found") -var Errinvalidcredentials = errors.New("invalid credentials") -var ErrDuplicateemail = errors.New("duplicate email") +import "github.com/stripe/stripe-go/v78" +import "github.com/stripe/stripe-go/v78/customer" + +var ErrRoRecord = errors.New("no matching record found") +var ErrInvalidCredentials = errors.New("invalid credentials") +var ErrDuplicateEmail = errors.New("duplicate email") type Account struct { Id int32 @@ -24,6 +27,7 @@ type Account struct { Lastname string Email string Created time.Time + StripeID string } type Usermodel struct { @@ -31,23 +35,53 @@ type Usermodel struct { } func (m *Usermodel) Insert(username string, password string, firstname string, lastname string, email string) error { - hashedpassword, err := bcrypt.GenerateFromPassword([]byte(password), 12) - - stmt := `INSERT INTO accounts (username, password, firstname, lastname, email, created) VALUES ($1, $2, $3, $4, $5, NOW());` - - _, err = m.DB.Exec(stmt, username, string(hashedpassword), firstname, lastname, email) - if err != nil { + params := &stripe.CustomerParams{ + Name: stripe.String(fmt.Sprintf("%s %s", firstname, lastname)), + Email: stripe.String(email), } + customer, err := customer.New(params) + + hashedpassword, err := bcrypt.GenerateFromPassword([]byte(password), 12) + stmt := `INSERT INTO accounts (username, password, firstname, lastname, email, created, stripe_id) VALUES ($1, $2, $3, $4, $5, NOW(), $6);` + + _, err = m.DB.Exec(stmt, username, string(hashedpassword), firstname, lastname, email, customer.ID) + if err != nil { + fmt.Println(err) + } + + + return nil +} + +func (m *Usermodel) Delete(id int32) error { + account, err := users.Get_account(id) + result, err := customer.Del(account.StripeID, nil) + if err != nil { + fmt.Println(err) + } + fmt.Println(result) + stmt := `DELETE FROM accounts WHERE id = $1;` + + _, err = m.DB.Exec(stmt, id) + if err != nil { + fmt.Println(err) + } + return nil } func (m *Usermodel) Get_account(id int32) (Account, error) { - stmt := `SELECT id, username, password, color, firstname, lastname, email, created FROM accounts WHERE id = $1;` + if id == 0 { + return Account{}, nil + } + stmt := `SELECT id, username, password, color, firstname, lastname, email, created, stripe_id FROM accounts WHERE id = $1;` row := m.DB.QueryRow(stmt, id) + var account Account - err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created) + err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created, &account.StripeID) + if err == sql.ErrNoRows { return Account{}, sql.ErrNoRows } else if err != nil { @@ -62,10 +96,13 @@ func (m *Usermodel) Authenticate(username string, password string) (int32, error var hashedpassword []byte row := m.DB.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username) err := row.Scan(&id, &hashedpassword) - err = bcrypt.CompareHashAndPassword(hashedpassword, []byte(password)) + if err == sql.ErrNoRows { + return 0, ErrInvalidCredentials + } + err = bcrypt.CompareHashAndPassword(hashedpassword, []byte(password)) if err == bcrypt.ErrMismatchedHashAndPassword { - return 0, bcrypt.ErrMismatchedHashAndPassword + return 0, ErrInvalidCredentials } else if err != nil { return 0, err } diff --git a/ui/account.html b/ui/account.html index 5d50e6e..0a03bbc 100644 --- a/ui/account.html +++ b/ui/account.html @@ -5,4 +5,12 @@
Last name: {{.Account.Lastname}}
Color: {{.Account.Color}}
+ +
+
+ +
+
{{end}} diff --git a/ui/login.html b/ui/login.html index 8d3c3fe..ed38137 100644 --- a/ui/login.html +++ b/ui/login.html @@ -3,6 +3,10 @@

Log in

+ {{with .FormErrors.generic}} + + {{end}} + {{with .FormErrors.username}} {{end}} diff --git a/ui/register.html b/ui/register.html index 089e96d..66777f5 100644 --- a/ui/register.html +++ b/ui/register.html @@ -3,11 +3,11 @@

Register

- {{with .Formerrors.username}} + {{with .FormErrors.username}} {{end}} -
+

@@ -28,11 +28,11 @@

- {{with .Formerrors.password}} + {{with .FormErrors.password}} {{end}} -
+