login-{validator,app}: Check if the user is already logged elsewhere
This commit is contained in:
parent
f5bac225f0
commit
c313debfdc
4 changed files with 93 additions and 12 deletions
26
pkg/login-app/cmd/dialog-forcelogin.go
Normal file
26
pkg/login-app/cmd/dialog-forcelogin.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
func CreateForceLoginDialog(app *tview.Application, username, password string, next func(string, string, *bool)) {
|
||||
modal := tview.NewModal().
|
||||
SetText("You are already registered on a different machine.\n\nIf you continue, the other machine will no longer be able to use its dedicated IPs due to network safeties in place.\n\nIf the other machine doesn't work or you are Ok to lost your progression, just force this host as your new main host.\nIf you want a secondary machine to play without erasing your progression on the main one, continue without enforcing network safety.").
|
||||
AddButtons([]string{"Cancel", "Force this host as main", "Boot without network safety"}).
|
||||
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
||||
var force bool
|
||||
if buttonLabel == "Force this host as main" {
|
||||
force = true
|
||||
next(username, password, &force)
|
||||
} else if buttonLabel == "Boot without network safety" {
|
||||
force = false
|
||||
next(username, password, &force)
|
||||
} else {
|
||||
askLogin(app)
|
||||
}
|
||||
})
|
||||
|
||||
app.SetRoot(modal, true)
|
||||
app.SetFocus(modal)
|
||||
}
|
||||
|
|
@ -8,21 +8,24 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func checkLogin(username, password string) (bool, error) {
|
||||
j, err := json.Marshal(map[string]string{
|
||||
"username": username,
|
||||
"password": password,
|
||||
})
|
||||
type loginForm struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Force *bool `json:"force,omitempty"`
|
||||
}
|
||||
|
||||
func checkLogin(username, password string, force *bool) (int, error) {
|
||||
j, err := json.Marshal(loginForm{Username: username, Password: password, Force: force})
|
||||
if err != nil {
|
||||
return false, err
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resp, err := http.Post(URLLogin, "application/json", bytes.NewReader(j))
|
||||
if err != nil {
|
||||
return false, err
|
||||
return 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
cnt, _ := ioutil.ReadAll(resp.Body)
|
||||
return resp.StatusCode == http.StatusOK, errors.New(string(cnt))
|
||||
return resp.StatusCode, errors.New(string(cnt))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
|
|
@ -26,18 +27,25 @@ func modal(p tview.Primitive, width, height int) tview.Primitive {
|
|||
}
|
||||
|
||||
func askLogin(app *tview.Application) {
|
||||
CreateLoginDialog(app, func(username, password string) {
|
||||
var afterLogin func(username, password string, force *bool)
|
||||
afterLogin = func(username, password string, force *bool) {
|
||||
// Display check dialog
|
||||
CreateCheckDialog(app)
|
||||
|
||||
go func() {
|
||||
if ok, err := checkLogin(username, password); ok {
|
||||
if status, err := checkLogin(username, password, force); status == http.StatusOK {
|
||||
loggedAs = username
|
||||
app.Stop()
|
||||
} else if status == http.StatusPaymentRequired {
|
||||
CreateForceLoginDialog(app, username, password, afterLogin)
|
||||
} else {
|
||||
CreateErrMsgDialog(app, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
CreateLoginDialog(app, func(username, password string) {
|
||||
afterLogin(username, password, nil)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue