git-svn-id: svn://losandesgames.com/alfheim-website@11 15359d88-9307-4e75-a9c1-e5686e5897df
This commit is contained in:
parent
e27424c0f5
commit
f4862b4f5d
52
handlers.go
52
handlers.go
@ -60,14 +60,14 @@ func home(w http.ResponseWriter, r *http.Request) {
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
|
||||||
err = text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
err = text.Execute(w, templatedata{})
|
err := text.Execute(w, templatedata{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
@ -85,7 +85,7 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
|
||||||
text.Execute(w, templatedata{})
|
err := text.Execute(w, templatedata{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
@ -111,14 +111,27 @@ func login(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if len(errors) > 0 {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
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 {
|
if id > 0 {
|
||||||
session.Values["id"] = id
|
session.Values["id"] = id
|
||||||
fmt.Println("Logged in with id:", id)
|
fmt.Println("Logged in with id:", id)
|
||||||
@ -141,7 +154,7 @@ func logout(w http.ResponseWriter, r *http.Request) {
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
|
||||||
text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
@ -163,10 +176,13 @@ func register(w http.ResponseWriter, r *http.Request) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
id := authenticated_user(r)
|
||||||
|
account, err := users.Get_account(id)
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
|
||||||
text.Execute(w, templatedata{})
|
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
@ -191,7 +207,7 @@ func register(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if len(errors) > 0 {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
http.Error(w, "Internal Server Error", 500)
|
||||||
@ -226,7 +242,7 @@ func account(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
err := text.Execute(w, templatedata{AuthenticatedUser: id, Account: account})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
http.Error(w, "Internal Server Error", 500)
|
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)
|
// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
8
main.go
8
main.go
@ -12,7 +12,7 @@ import _ "github.com/lib/pq"
|
|||||||
import "database/sql"
|
import "database/sql"
|
||||||
import "github.com/gorilla/sessions"
|
import "github.com/gorilla/sessions"
|
||||||
import "regexp"
|
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"
|
||||||
import "github.com/stripe/stripe-go/v78/customer"
|
import "github.com/stripe/stripe-go/v78/customer"
|
||||||
@ -54,6 +54,10 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("Hello, Sailor!")
|
fmt.Println("Hello, Sailor!")
|
||||||
|
|
||||||
|
h, _ := bcrypt.GenerateFromPassword([]byte("password"), 12)
|
||||||
|
|
||||||
|
fmt.Println(string(h))
|
||||||
|
|
||||||
stripe.Key = "sk_test_51PGebgKUHKCjyTmc97rfDPcvew6EhqDz2qp3U7XoAMIilAU9IVo2NO4P7ylkTvbBafFVr94trha1VYY32jRWMw2K00Yq7YJXFf"
|
stripe.Key = "sk_test_51PGebgKUHKCjyTmc97rfDPcvew6EhqDz2qp3U7XoAMIilAU9IVo2NO4P7ylkTvbBafFVr94trha1VYY32jRWMw2K00Yq7YJXFf"
|
||||||
|
|
||||||
c, _ := customer.Get("cus_Q7am78hLcLUvGQ", nil)
|
c, _ := customer.Get("cus_Q7am78hLcLUvGQ", nil)
|
||||||
@ -106,7 +110,7 @@ func main() {
|
|||||||
mux.HandleFunc("/logout", logout)
|
mux.HandleFunc("/logout", logout)
|
||||||
mux.HandleFunc("/register", register)
|
mux.HandleFunc("/register", register)
|
||||||
mux.HandleFunc("/account", require_authenticated_user(account))
|
mux.HandleFunc("/account", require_authenticated_user(account))
|
||||||
|
mux.HandleFunc("/deleteaccount", require_authenticated_user(deleteaccount))
|
||||||
|
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(*addr, secure_headers(mux)))
|
log.Fatal(http.ListenAndServe(*addr, secure_headers(mux)))
|
||||||
|
|||||||
65
models.go
65
models.go
@ -9,11 +9,14 @@ import "time"
|
|||||||
import "golang.org/x/crypto/bcrypt"
|
import "golang.org/x/crypto/bcrypt"
|
||||||
import "database/sql"
|
import "database/sql"
|
||||||
import _ "github.com/lib/pq"
|
import _ "github.com/lib/pq"
|
||||||
import _ "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var Errnorecord = errors.New("no matching record found")
|
import "github.com/stripe/stripe-go/v78"
|
||||||
var Errinvalidcredentials = errors.New("invalid credentials")
|
import "github.com/stripe/stripe-go/v78/customer"
|
||||||
var ErrDuplicateemail = errors.New("duplicate email")
|
|
||||||
|
var ErrRoRecord = errors.New("no matching record found")
|
||||||
|
var ErrInvalidCredentials = errors.New("invalid credentials")
|
||||||
|
var ErrDuplicateEmail = errors.New("duplicate email")
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Id int32
|
Id int32
|
||||||
@ -24,6 +27,7 @@ type Account struct {
|
|||||||
Lastname string
|
Lastname string
|
||||||
Email string
|
Email string
|
||||||
Created time.Time
|
Created time.Time
|
||||||
|
StripeID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Usermodel struct {
|
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 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Usermodel) Get_account(id int32) (Account, error) {
|
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)
|
row := m.DB.QueryRow(stmt, id)
|
||||||
|
|
||||||
var account Account
|
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 {
|
if err == sql.ErrNoRows {
|
||||||
return Account{}, sql.ErrNoRows
|
return Account{}, sql.ErrNoRows
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
@ -62,10 +96,13 @@ func (m *Usermodel) Authenticate(username string, password string) (int32, error
|
|||||||
var hashedpassword []byte
|
var hashedpassword []byte
|
||||||
row := m.DB.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username)
|
row := m.DB.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username)
|
||||||
err := row.Scan(&id, &hashedpassword)
|
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 {
|
if err == bcrypt.ErrMismatchedHashAndPassword {
|
||||||
return 0, bcrypt.ErrMismatchedHashAndPassword
|
return 0, ErrInvalidCredentials
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,4 +5,12 @@
|
|||||||
<div>Last name: {{.Account.Lastname}}</div>
|
<div>Last name: {{.Account.Lastname}}</div>
|
||||||
<div>Color: {{.Account.Color}}</div>
|
<div>Color: {{.Account.Color}}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="wrapper">
|
||||||
|
<form action="/deleteaccount" method="post">
|
||||||
|
<div class="login-btn-wrapper">
|
||||||
|
<input type="submit" value="Delete Account" class="btn">
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
@ -3,6 +3,10 @@
|
|||||||
<form method="POST">
|
<form method="POST">
|
||||||
<h1>Log in</h1>
|
<h1>Log in</h1>
|
||||||
|
|
||||||
|
{{with .FormErrors.generic}}
|
||||||
|
<label class="error">{{.}}</label>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{with .FormErrors.username}}
|
{{with .FormErrors.username}}
|
||||||
<label class="error">{{.}}</label>
|
<label class="error">{{.}}</label>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
@ -3,11 +3,11 @@
|
|||||||
<form method="POST">
|
<form method="POST">
|
||||||
<h1>Register</h1>
|
<h1>Register</h1>
|
||||||
|
|
||||||
{{with .Formerrors.username}}
|
{{with .FormErrors.username}}
|
||||||
<label class="error">{{.}}</label>
|
<label class="error">{{.}}</label>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<div class="input-box" {{with .Formerrors.username}}class="input-error"{{end}}>
|
<div class="input-box" {{with .FormErrors.username}}class="input-error"{{end}}>
|
||||||
<input type="text" id="username" name="username" placeholder="Username" required>
|
<input type="text" id="username" name="username" placeholder="Username" required>
|
||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
@ -28,11 +28,11 @@
|
|||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
|
|
||||||
{{with .Formerrors.password}}
|
{{with .FormErrors.password}}
|
||||||
<label class="error">{{.}}</label>
|
<label class="error">{{.}}</label>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<div class="input-box" {{with .Formerrors.password}}class="input-error"{{end}}>
|
<div class="input-box" {{with .FormErrors.password}}class="input-error"{{end}}>
|
||||||
<input type="password" id="password" name="password" placeholder="Password" required>
|
<input type="password" id="password" name="password" placeholder="Password" required>
|
||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user