git-svn-id: svn://losandesgames.com/alfheim-website@2 15359d88-9307-4e75-a9c1-e5686e5897df

This commit is contained in:
Vicente Ferrari Smith 2024-05-14 11:50:23 +00:00
parent de52d92eb7
commit c98e6b56bd
4 changed files with 73 additions and 30 deletions

View File

@ -1,6 +1,8 @@
{{define "body"}} {{define "body"}}
hello <div class="account-wrapper">
<div>{{.Username}}</div> <div>Username: {{.Username}}</div>
<div>{{.Color}}</div> <div>First name: {{.Firstname}}</div>
whut <div>Last name: {{.Lastname}}</div>
<div>Color: {{.Color}}</div>
</div>
{{end}} {{end}}

View File

@ -4,10 +4,12 @@ import "fmt"
import "log" import "log"
import "net/http" import "net/http"
import "html/template" import "html/template"
import "strconv" //import "strconv"
import "strings" import "strings"
import "unicode/utf8" import "unicode/utf8"
import "alfheimgame/models" import "alfheimgame/models"
import "errors"
import "runtime/debug"
type templatedata struct { type templatedata struct {
Formerrors map[string]string Formerrors map[string]string
@ -66,6 +68,7 @@ func login(w http.ResponseWriter, r *http.Request) {
} }
case http.MethodPost: case http.MethodPost:
session, _ := store.Get(r, "id");
logindata := LoginData{username: r.FormValue("username"), password: r.FormValue("password")} logindata := LoginData{username: r.FormValue("username"), password: r.FormValue("password")}
errors := make(map[string]string) errors := make(map[string]string)
@ -90,6 +93,13 @@ func login(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", 500) http.Error(w, "Internal Server Error", 500)
} }
} }
id, _ := users.Authenticate(logindata.username, logindata.password)
if id > 0 {
session.Values["id"] = id
session.Save(r, w)
http.Redirect(w, r, "/account", http.StatusSeeOther)
}
} }
} }
@ -143,30 +153,41 @@ func register(w http.ResponseWriter, r *http.Request) {
} }
func account(w http.ResponseWriter, r *http.Request) { func account(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id")) session, _ := store.Get(r, "id")
if err != nil || id < 1 { //id, err := strconv.Atoi(r.URL.Query().Get("id"))
http.NotFound(w, r) //if err != nil || id < 1 {
return // http.NotFound(w, r)
} // return
account, err := users.Get_account(int32(id)); //}
if err != nil { //account, err := users.Get_account(int32(id));
log.Fatal(err); //if err != nil {
} // log.Fatal(err);
//}
text, err := template.ParseFiles("base.html", "account/index.html") id, ok := session.Values["id"].(int32)
if err != nil { if !ok {
http.Error(w, "Internal Server Error", 500) trace := fmt.Sprintf("%s\n%s", errors.New("type assertion to int32 failed").Error(), debug.Stack())
log.Fatal(err) log.Println(trace)
} http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
} else {
account, err := users.Get_account(id)
fmt.Println(account)
switch r.Method { text, err := template.ParseFiles("base.html", "account/index.html")
case http.MethodGet:
text.Execute(w, account) if err != nil {
if err != nil { http.Error(w, "Internal Server Error", 500)
log.Fatal(err) log.Fatal(err)
http.Error(w, "Internal Server Error", 500) }
}
fmt.Printf("executed"); switch r.Method {
case http.MethodGet:
text.Execute(w, account)
if err != nil {
log.Fatal(err)
http.Error(w, "Internal Server Error", 500)
}
}
//case http.MethodPost: //case http.MethodPost:
// data := LoginData{username: r.FormValue("username"), password: r.FormValue("password")} // data := LoginData{username: r.FormValue("username"), password: r.FormValue("password")}

View File

@ -5,6 +5,7 @@ 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"
var Errnorecord = errors.New("no matching record found") var Errnorecord = errors.New("no matching record found")
var Errinvalidcredentials = errors.New("invalid credentials") var Errinvalidcredentials = errors.New("invalid credentials")
@ -39,10 +40,11 @@ func (m *Usermodel) Insert(username string, password string, firstname string, l
} }
func (m *Usermodel) Get_account(id int32) (Account, error) { func (m *Usermodel) Get_account(id int32) (Account, error) {
stmt := `SELECT id, username, password, color FROM accounts WHERE id = $1;` stmt := `SELECT id, username, password, color, firstname, lastname, email, created 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) err := row.Scan(&account.Id, &account.Username, &account.Password, &account.Color, &account.Firstname, &account.Lastname, &account.Email, &account.Created)
fmt.Println(err)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return Account{}, sql.ErrNoRows return Account{}, sql.ErrNoRows
} else if err != nil { } else if err != nil {
@ -52,10 +54,18 @@ func (m *Usermodel) Get_account(id int32) (Account, error) {
return account, nil return account, nil
} }
func (m *Usermodel) Authenticate(username string, password string) (int, error) { func (m *Usermodel) Authenticate(username string, password string) (int32, error) {
var id int32 var id int32
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 == bcrypt.ErrMismatchedHashAndPassword {
return 0, bcrypt.ErrMismatchedHashAndPassword
} else if err != nil {
return 0, err
}
return id, nil
} }

View File

@ -55,6 +55,16 @@ main {
margin-right: auto; margin-right: auto;
} }
.account-wrapper {
background: transparent;
border: 2px solid white;
backdrop-filter: blur(20px);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 15px;
padding: 30px 40px;
font-size: x-large;
}
.wrapper { .wrapper {
background: transparent; background: transparent;
border: 2px solid white; border: 2px solid white;