registration and logging in bootleg but functionining
parent
1259354a3c
commit
66471f74d2
|
@ -131,7 +131,7 @@ func _main() error {
|
|||
|
||||
mainPage := tview.NewList().
|
||||
AddItem("jack in", "connect using an existing account", '1', func() {
|
||||
pages.SwitchToPage("game")
|
||||
pages.SwitchToPage("login")
|
||||
}).
|
||||
AddItem("rez a toon", "create a new account", '2', func() {
|
||||
pages.SwitchToPage("register")
|
||||
|
@ -143,30 +143,61 @@ func _main() error {
|
|||
|
||||
pages.AddPage("main", mainPage, true, false)
|
||||
|
||||
unfi := tview.NewInputField().SetLabel("account name")
|
||||
pwfi := tview.NewInputField().SetLabel("password").SetMaskCharacter('~')
|
||||
lunfi := tview.NewInputField().SetLabel("account name")
|
||||
lpwfi := tview.NewInputField().SetLabel("password").SetMaskCharacter('~')
|
||||
|
||||
registerPage := tview.NewForm().AddFormItem(unfi).AddFormItem(pwfi).
|
||||
loginPage := tview.NewForm().AddFormItem(lunfi).AddFormItem(lpwfi).
|
||||
SetCancelFunc(func() {
|
||||
pages.SwitchToPage("main")
|
||||
})
|
||||
|
||||
submitFunc := func() {
|
||||
loginSubmitFn := func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
si, err := cs.Client.Register(ctx, &proto.AuthInfo{
|
||||
Username: unfi.GetText(),
|
||||
Password: pwfi.GetText(),
|
||||
si, err := cs.Client.Login(ctx, &proto.AuthInfo{
|
||||
Username: lunfi.GetText(),
|
||||
Password: lpwfi.GetText(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
fmt.Printf("DBG %#v\n", si)
|
||||
fmt.Printf("DBG %#v\n", err)
|
||||
cs.SessionInfo = si
|
||||
|
||||
pages.SwitchToPage("game")
|
||||
}
|
||||
|
||||
registerPage.AddButton("gimme that shit", submitFunc)
|
||||
loginPage.AddButton("gimme that shit", loginSubmitFn)
|
||||
loginPage.AddButton("nah get outta here", func() {
|
||||
pages.SwitchToPage("main")
|
||||
})
|
||||
pages.AddPage("login", loginPage, true, false)
|
||||
|
||||
runfi := tview.NewInputField().SetLabel("account name")
|
||||
rpwfi := tview.NewInputField().SetLabel("password").SetMaskCharacter('~')
|
||||
|
||||
registerPage := tview.NewForm().AddFormItem(runfi).AddFormItem(rpwfi).
|
||||
SetCancelFunc(func() {
|
||||
pages.SwitchToPage("main")
|
||||
})
|
||||
|
||||
registerSubmitFn := func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
si, err := cs.Client.Register(ctx, &proto.AuthInfo{
|
||||
Username: runfi.GetText(),
|
||||
Password: rpwfi.GetText(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
cs.SessionInfo = si
|
||||
|
||||
pages.SwitchToPage("game")
|
||||
}
|
||||
|
||||
registerPage.AddButton("gimme that shit", registerSubmitFn)
|
||||
registerPage.AddButton("nah get outta here", func() {
|
||||
pages.SwitchToPage("main")
|
||||
})
|
||||
|
|
|
@ -84,13 +84,14 @@ func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld
|
|||
}
|
||||
|
||||
func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
|
||||
err = db.CreateAccount(auth.Username, auth.Password)
|
||||
var a *db.Account
|
||||
a, err = db.CreateAccount(auth.Username, auth.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var sessionID string
|
||||
sessionID, err = db.StartSession(auth.Username)
|
||||
sessionID, err = db.StartSession(*a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -101,15 +102,17 @@ func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (s
|
|||
}
|
||||
|
||||
func (s *gameWorldServer) Login(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
|
||||
err = db.ValidateCredentials(auth.Username, auth.Password)
|
||||
fmt.Printf("DBG %#v\n", "HI")
|
||||
var a *db.Account
|
||||
a, err = db.ValidateCredentials(auth.Username, auth.Password)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var sessionID string
|
||||
sessionID, err = db.StartSession(auth.Username)
|
||||
sessionID, err = db.StartSession(*a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
si = &proto.SessionInfo{SessionID: sessionID}
|
||||
|
|
|
@ -26,37 +26,49 @@ func connect() (*pgxpool.Pool, error) {
|
|||
return conn, nil
|
||||
}
|
||||
|
||||
func CreateAccount(name, password string) error {
|
||||
func CreateAccount(name, password string) (*Account, error) {
|
||||
conn, err := connect()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = conn.Exec(context.Background(), "INSERT INTO accounts VALUES ( ?, ? )", name, password)
|
||||
_, err = conn.Exec(context.Background(),
|
||||
"INSERT INTO accounts (name, pwhash) VALUES ( $1, $2 )", name, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
row := conn.QueryRow(context.Background(), "SELECT id,name,pwhash FROM accounts WHERE name = $1")
|
||||
a := &Account{}
|
||||
err = row.Scan(&a.ID, &a.Name, &a.Pwhash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO handle and cleanup unqiue violations
|
||||
|
||||
return err
|
||||
return a, err
|
||||
}
|
||||
|
||||
func ValidateCredentials(name, password string) error {
|
||||
func ValidateCredentials(name, password string) (*Account, error) {
|
||||
a, err := GetAccount(name)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO hashing lol
|
||||
|
||||
if a.Password != password {
|
||||
return errors.New("invalid credentials")
|
||||
if a.Pwhash != password {
|
||||
return nil, errors.New("invalid credentials")
|
||||
}
|
||||
|
||||
return nil
|
||||
return a, nil
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
Name string
|
||||
Password string
|
||||
ID int
|
||||
Name string
|
||||
Pwhash string
|
||||
}
|
||||
|
||||
func GetAccount(name string) (*Account, error) {
|
||||
|
@ -65,11 +77,11 @@ func GetAccount(name string) (*Account, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
row := conn.QueryRow(context.Background(), "SELECT name,password FROM accounts WHERE name = ?", name)
|
||||
row := conn.QueryRow(context.Background(), "SELECT id, name, pwhash FROM accounts WHERE name = $1", name)
|
||||
|
||||
a := &Account{}
|
||||
|
||||
err = row.Scan(&a.Name, &a.Password)
|
||||
err = row.Scan(&a.ID, &a.Name, &a.Pwhash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -77,7 +89,7 @@ func GetAccount(name string) (*Account, error) {
|
|||
return a, nil
|
||||
}
|
||||
|
||||
func StartSession(name string) (sessionID string, err error) {
|
||||
func StartSession(a Account) (sessionID string, err error) {
|
||||
var conn *pgxpool.Pool
|
||||
conn, err = connect()
|
||||
if err != nil {
|
||||
|
@ -86,7 +98,7 @@ func StartSession(name string) (sessionID string, err error) {
|
|||
|
||||
sessionID = uuid.New().String()
|
||||
|
||||
_, err = conn.Exec(context.Background(), "INSERT INTO sessions VALUES ( ?, ? )", name, sessionID)
|
||||
_, err = conn.Exec(context.Background(), "INSERT INTO sessions (session_id, account) VALUES ( $1, $2 )", sessionID, a.ID)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue