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

This commit is contained in:
vfs committed 2024-05-14 11:50:23 +00:00
1 parent de52d92eb7
commit c98e6b56bd
4 files changed
+73 -30

No files matched your search

+13 -3
View File
@@ -5,6 +5,7 @@ import "time"
import "golang.org/x/crypto/bcrypt"
import "database/sql"
import _ "github.com/lib/pq"
import "fmt"
var Errnorecord = errors.New("no matching record found")
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) {
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)
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 {
return Account{}, sql.ErrNoRows
} else if err != nil {
@@ -52,10 +54,18 @@ func (m *Usermodel) Get_account(id int32) (Account, error) {
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 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 == bcrypt.ErrMismatchedHashAndPassword {
return 0, bcrypt.ErrMismatchedHashAndPassword
} else if err != nil {
return 0, err
}
return id, nil
}