fix(gateway): show the login when the token is rejected

Closes #843
This commit is contained in:
ayn2op 2026-10-12 12:37:43 -04:00
parent 4534f130b3
commit 817306b1ad
3 changed files with 30 additions and 1 deletions

View file

@ -16,5 +16,5 @@ func New(id discordgateway.Identifier) *discordgateway.Gateway {
conn := ws.NewConnWithDialer(codec, NewDialer())
socket := ws.NewCustomWebsocket(conn, discordgateway.AddGatewayParams(gatewayURL))
return discordgateway.FromWebsocketGateway(ws.NewGateway(socket, nil), discordgateway.State{Identifier: id})
return discordgateway.FromWebsocketGateway(ws.NewGateway(socket, &discordgateway.DefaultGatewayOpts), discordgateway.State{Identifier: id})
}

View file

@ -0,0 +1,17 @@
package gateway
import (
"testing"
discordgateway "github.com/ayn2op/arikawa/v3/gateway"
"github.com/ayn2op/arikawa/v3/utils/ws"
)
func TestNew(t *testing.T) {
t.Run("authentication failed", func(t *testing.T) {
opts := New(discordgateway.DefaultIdentifier("token")).Opts()
if !opts.ErrorIsFatalClose(&ws.CloseEvent{Code: 4004}) {
t.Error("gateway reconnects after the token is rejected")
}
})
}

View file

@ -2,22 +2,34 @@ package chat
import (
"context"
"errors"
"log/slog"
"time"
"github.com/ayn2op/arikawa/v3/discord"
"github.com/ayn2op/arikawa/v3/gateway"
"github.com/ayn2op/arikawa/v3/utils/ws"
"github.com/ayn2op/discordo/internal/ui/chat/composer"
"github.com/ayn2op/discordo/internal/ui/chat/guildstree"
"github.com/ayn2op/ningen/v3"
"github.com/ayn2op/tview"
)
// closeCodeAuthenticationFailed is the close code of the gateway for a token it rejects.
const closeCodeAuthenticationFailed = 4004
func openState(state *ningen.State) tview.Cmd {
return func() tview.Msg {
if err := state.Open(context.Background()); err != nil {
slog.Error("failed to open chat state", "err", err)
// A rejected token never works again, so ask for a new one.
var event *ws.CloseEvent
if errors.As(err, &event) && event.Code == closeCodeAuthenticationFailed {
return LogoutMsg{}
}
}
return nil
}
}