Skip to content

Commit

Permalink
Updated Launcher with saving/loading of a token from homedir (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennycoder authored Mar 2, 2023
1 parent 502c56f commit 1824c76
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 71 additions & 9 deletions game/launcher/main.go → game/GameLauncher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"runtime"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
Expand Down Expand Up @@ -50,6 +51,8 @@ func main() {
os.Exit(1)
}

myToken = ""

// Callback handling from the frontend api
http.HandleFunc("/callback", handleGoogleCallback)
go func() {
Expand Down Expand Up @@ -78,6 +81,13 @@ func main() {
grid := container.New(layout.NewGridLayout(1), image, subGrid)

myWindow.SetContent(grid)

// If we have a valid token, let's use it and update the UI right away
if loadToken() {
playerName := getPlayerName()
updateUI(playerName)
}

myWindow.ShowAndRun()
}

Expand All @@ -99,9 +109,24 @@ func handleGoogleCallback(rw http.ResponseWriter, req *http.Request) {
log.Fatal("No token received!")
}

saveToken(myToken)

// Update UI with profile info and launch game button
playerName := getPlayerName()
updateUI(playerName)

// Close the browser window
closeScript := `<script>
setTimeout("window.close()",3000)
</script>
<p>
<h2>Authenticated successfully. Please return to your application. This tab will close in 3 seconds.</h2>
</p>`
fmt.Fprintf(rw, closeScript)
}

func updateUI(playerName string) {
// Update UI with profile info and launch game button
image := canvas.NewImageFromFile("assets/header.png")
image.FillMode = canvas.ImageFillContain

Expand All @@ -124,15 +149,6 @@ func handleGoogleCallback(rw http.ResponseWriter, req *http.Request) {
subGrid := container.New(layout.NewGridLayout(1), infoGrid, buttonPlay, buttonExit)
grid := container.New(layout.NewGridLayout(1), image, subGrid)
myWindow.SetContent(grid)

// Close the browser window
closeScript := `<script>
setTimeout("window.close()",3000)
</script>
<p>
<h2>Authenticated successfully. Please return to your application. This tab will close in 3 seconds.</h2>
</p>`
fmt.Fprintf(rw, closeScript)
}

func handlePlay() {
Expand Down Expand Up @@ -202,3 +218,49 @@ func openBrowser(url string) {
log.Fatal(err)
}
}

func saveToken(token string) {
dirname, err := os.UserHomeDir()
filename := dirname + "/droidshooter.jwt"

if err != nil {
log.Fatal(err)
}

err = os.WriteFile(filename, []byte(token), 0644)
if err != nil {
log.Fatal(err)
}
}

func loadToken() bool {
dirname, err := os.UserHomeDir()
filename := dirname + "/droidshooter.jwt"

if err != nil {
log.Fatal(err)
}

data, err := os.ReadFile(filename)
if err != nil {
return false
}

file, err := os.Stat(filename)
if err != nil {
log.Fatal(err)
}

modifiedtime := file.ModTime()
in30Days := time.Now().Add(24 * time.Hour * 30)

if modifiedtime.After(in30Days) {
log.Printf("Token is old. Deleting.")
os.Remove(filename)
return false
}

myToken = string(data)
log.Printf("Token loaded from file")
return true
}

0 comments on commit 1824c76

Please sign in to comment.