Skip to content

feat: add feature to show warning to user if they are already logged in #14219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,12 @@ func (r *RootCmd) login() *serpent.Command {
useTokenForSession bool
)
cmd := &serpent.Command{
Use: "login [<url>]",
Short: "Authenticate with Coder deployment",
Middleware: serpent.RequireRangeArgs(0, 1),
Use: "login [<url>]",
Short: "Authenticate with Coder deployment",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(0, 1),
r.CheckExistingUserSession(),
),
Handler: func(inv *serpent.Invocation) error {
ctx := inv.Context()
rawURL := ""
Expand Down
44 changes: 44 additions & 0 deletions cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,48 @@ func TestLogin(t *testing.T) {
require.NoError(t, err)
require.Equal(t, selected, first.OrganizationID.String())
})

t.Run("ExistingUserValidTokenTTYWarnReloggin", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)

doneChan := make(chan struct{})
root, _ := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open")
pty := ptytest.New(t).Attach(root)
go func() {
defer close(doneChan)
err := root.Run()
assert.NoError(t, err)
}()

pty.ExpectMatch(fmt.Sprintf("Attempting to authenticate with argument URL: '%s'", client.URL.String()))
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
if runtime.GOOS != "windows" {
// For some reason, the match does not show up on Windows.
pty.ExpectMatch(client.SessionToken())
}
pty.ExpectMatch("Welcome to Coder")
<-doneChan

doneChan = make(chan struct{})
root, _ = clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open")
pty = ptytest.New(t).Attach(root)
go func() {
defer close(doneChan)
err := root.Run()
assert.NoError(t, err)
}()
pty.ExpectMatch(fmt.Sprintf("You are already logged in to '%s'!", client.URL.String()))
pty.ExpectMatch(fmt.Sprintf("Attempting to authenticate with argument URL: '%s'", client.URL.String()))
pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken())
if runtime.GOOS != "windows" {
// For some reason, the match does not show up on Windows.
pty.ExpectMatch(client.SessionToken())
}
pty.ExpectMatch("Welcome to Coder")
<-doneChan
})
}
44 changes: 44 additions & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,50 @@ func (r *RootCmd) InitClient(client *codersdk.Client) serpent.MiddlewareFunc {
}
}

func (r *RootCmd) CheckExistingUserSession() serpent.MiddlewareFunc {
return func(next serpent.HandlerFunc) serpent.HandlerFunc {
return func(inv *serpent.Invocation) error {
conf := r.createConfig()
var err error
// Read the client URL stored on disk.
if r.clientURL == nil || r.clientURL.String() == "" {
rawURL, err := conf.URL().Read()
// If the configuration files are absent, the user is logged out
if os.IsNotExist(err) {
return next(inv)
}
if err != nil {
return err
}

r.clientURL, err = url.Parse(strings.TrimSpace(rawURL))
if err != nil {
return err
}
}
// Read the token stored on disk.
if r.token == "" {
r.token, err = conf.Session().Read()
// Even if there isn't a token, we don't care.
if err != nil && !os.IsNotExist(err) {
return next(inv)
}
}

// IF r.token and r.clientUR, exists then print warning "You are already logged in to $"
if r.token != "" && r.clientURL.String() != "" {
_, _ = fmt.Fprintf(inv.Stderr, Caret+"%s!\n", pretty.Sprint(cliui.DefaultStyles.Warn, "You are already logged in to "+r.clientURL.String()+"!"))
}

// We remove the token and clientURL from the RootCmd, so the user can still go ahead and login
r.clientURL = nil
r.token = ""

return next(inv)
}
}
}

// HeaderTransport creates a new transport that executes `--header-command`
// if it is set to add headers for all outbound requests.
func (r *RootCmd) HeaderTransport(ctx context.Context, serverURL *url.URL) (*codersdk.HeaderTransport, error) {
Expand Down
Loading