Files
ax/src/store/session.go
T
eliaskohout 24fb3a8b62 fix: prevent segfault in login command when session doesn't exist
LoadSession() was returning nil when the session file didn't exist (first login).
The login command then tried to dereference nil when setting session.Token,
causing a panic. Now LoadSession() returns an empty Session with the path set,
so callers can always use the returned session.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-04-02 13:23:34 +02:00

55 lines
1.1 KiB
Go

package store
import (
"encoding/json"
"os"
"path/filepath"
)
// Session holds the server-issued token returned by POST /auth/poll.
// The ax server owns the full OIDC flow; the client only needs this token.
type Session struct {
path string
Token string `json:"token"`
}
func LoadSession() (*Session, error) {
sessionRoot, err := FindDataRoot(".local", "share")
if err != nil {
return nil, err
}
path := filepath.Join(sessionRoot, "session.json")
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return &Session{path: path}, nil
}
return nil, err
}
var s Session
if err := json.Unmarshal(data, &s); err != nil {
return nil, err
}
s.path = path
return &s, nil
}
func (s *Session) Save() error {
if err := os.MkdirAll(filepath.Dir(s.path), 0700); err != nil {
return err
}
data, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(s.path, data, 0600)
}
func (s *Session) ClearSession() error {
err := os.Remove(s.path)
if os.IsNotExist(err) {
return nil
}
return err
}