Remember password

Resolves #26.
This commit is contained in:
Trevor Slocum 2024-02-01 09:26:37 -08:00
parent 1f03a25fbb
commit cac48f46eb
5 changed files with 63 additions and 23 deletions

View file

@ -1,3 +1,6 @@
1.3.0:
- Remember password
1.2.9:
- Remember guest username

View file

@ -594,6 +594,7 @@ type Game struct {
LoadReplay []byte
savedUsername string
savedPassword string
initialized bool
loaded bool
@ -628,10 +629,9 @@ func NewGame() *Game {
debugImg: ebiten.NewImage(200, 200),
volume: 1,
savedUsername: loadUsername(),
Mutex: &sync.Mutex{},
}
g.savedUsername, g.savedPassword = loadCredentials()
g.tutorialFrame.SetPositionChildren(true)
game = g
@ -1246,9 +1246,10 @@ func (g *Game) initialize() {
g.setRoot(connectFrame)
username := loadUsername()
username, password := loadCredentials()
if username != "" {
g.connectUsername.SetText(username)
g.connectPassword.SetText(password)
etk.SetFocus(g.connectPassword)
} else {
etk.SetFocus(g.connectUsername)
@ -1372,7 +1373,11 @@ func (g *Game) handleEvent(e interface{}) {
if strings.HasPrefix(username, "Guest_") && !onlyNumbers.MatchString(username[6:]) {
username = username[6:]
}
go saveUsername(username)
password := g.connectPassword.Text()
if password == "" {
password = g.registerPassword.Text()
}
go saveCredentials(username, password)
areIs := "are"
if ev.Clients == 1 {

View file

@ -3,7 +3,7 @@
package game
import (
"bytes"
"bufio"
"fmt"
"log"
"os"
@ -62,19 +62,32 @@ func userConfigDir() string {
return "/data/data/com.rocket9labs.boxcars"
}
func loadUsername() string {
func loadCredentials() (string, string) {
configDir := userConfigDir()
buf, err := os.ReadFile(path.Join(configDir, "config"))
f, err := os.Open(path.Join(configDir, "config"))
if err != nil {
return ""
return "", ""
}
return string(bytes.TrimSpace(buf))
var i int
var username, password string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
switch i {
case 0:
username = scanner.Text()
case 1:
password = scanner.Text()
}
i++
}
return username, password
}
func saveUsername(username string) {
func saveCredentials(username string, password string) {
configDir := userConfigDir()
_ = os.MkdirAll(filepath.Dir(configDir), 0700)
_ = os.WriteFile(path.Join(configDir, "config"), []byte(username), 0600)
_ = os.WriteFile(path.Join(configDir, "config"), []byte(username+"\n"+password), 0600)
}
func saveReplay(id int, content []byte) error {

View file

@ -35,25 +35,38 @@ func userConfigDir() string {
return path.Join(configDir, "boxcars")
}
func loadUsername() string {
func loadCredentials() (string, string) {
configDir := userConfigDir()
if configDir == "" {
return ""
return "", ""
}
buf, err := os.ReadFile(path.Join(configDir, "config"))
f, err := os.Open(path.Join(configDir, "config"))
if err != nil {
return ""
return "", ""
}
return string(bytes.TrimSpace(buf))
var i int
var username, password string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
switch i {
case 0:
username = scanner.Text()
case 1:
password = scanner.Text()
}
i++
}
return username, password
}
func saveUsername(username string) {
func saveCredentials(username string, password string) {
configDir := userConfigDir()
if configDir == "" {
return
}
_ = os.MkdirAll(filepath.Dir(configDir), 0700)
_ = os.WriteFile(path.Join(configDir, "config"), []byte(username), 0600)
_ = os.WriteFile(path.Join(configDir, "config"), []byte(username+"\n"+password), 0600)
}
func saveReplay(id int, content []byte) error {

View file

@ -44,22 +44,28 @@ func focused() bool {
return hasFocus.Truthy()
}
func loadUsername() string {
func loadCredentials() (string, string) {
document := js.Global().Get("document")
header := http.Header{}
header.Add("Cookie", document.Get("cookie").String())
request := http.Request{Header: header}
var username, password string
for _, cookie := range request.Cookies() {
if cookie.Name == "boxcars_username" {
return cookie.Value
switch cookie.Name {
case "boxcars_username":
username = cookie.Value
case "boxcars_password":
password = cookie.Value
}
}
return ""
return username, password
}
func saveUsername(username string) {
func saveCredentials(username string, password string) {
document := js.Global().Get("document")
document.Set("cookie", fmt.Sprintf("boxcars_username=%s; path=/", username))
document.Set("cookie", fmt.Sprintf("boxcars_password=%s; path=/", password))
}
func saveReplay(id int, content []byte) error {