diff --git a/account.go b/account.go new file mode 100644 index 0000000..e0ab768 --- /dev/null +++ b/account.go @@ -0,0 +1,415 @@ +package main + +import ( + "crypto/rand" + "encoding/base64" + "encoding/json" + "net/http" + "time" +) + +type User struct { + Username string `json:"username"` + Password string `json:"password"` + Role string `json:"role"` +} + +type Session struct { + Token string + Username string + Role string + ExpiresAt time.Time +} + +type LoginRequest struct { + Username string `json:"username"` + Password string `json:"password"` +} + +type LoginResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + Token string `json:"token,omitempty"` + Role string `json:"role,omitempty"` +} + +// In-memory storage (replace with database in production) +var ( + users = map[string]User{ + "admin": { + Username: "admin", + Password: "admin123", // In production, use hashed passwords + Role: "admin", + }, + } + sessions = make(map[string]Session) +) + +func generateToken() (string, error) { + bytes := make([]byte, 32) + if _, err := rand.Read(bytes); err != nil { + return "", err + } + return base64.URLEncoding.EncodeToString(bytes), nil +} + +func handleLogin(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if r.Method == "GET" { + // Serve login page + tmpl := ` + +
+ + +Administrátorské rozhraní
+