Skip to content

Commit 8a5277e

Browse files
authored
fix: restore previous session on coder server --dev (#1821)
1 parent 7eacab8 commit 8a5277e

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

cli/server.go

+49-9
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,11 @@ func server() *cobra.Command {
336336
return xerrors.Errorf("generate random admin password for dev: %w", err)
337337
}
338338
}
339-
err = createFirstUser(cmd, client, config, devUserEmail, devUserPassword)
339+
restorePreviousSession, err := createFirstUser(logger, cmd, client, config, devUserEmail, devUserPassword)
340340
if err != nil {
341341
return xerrors.Errorf("create first user: %w", err)
342342
}
343+
defer restorePreviousSession()
343344
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", devUserEmail)
344345
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", devUserPassword)
345346
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
@@ -518,12 +519,14 @@ func server() *cobra.Command {
518519
return root
519520
}
520521

521-
func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root, email, password string) error {
522+
// createFirstUser creates the first user and sets a valid session.
523+
// Caller must call restorePreviousSession on server exit.
524+
func createFirstUser(logger slog.Logger, cmd *cobra.Command, client *codersdk.Client, cfg config.Root, email, password string) (func(), error) {
522525
if email == "" {
523-
return xerrors.New("email is empty")
526+
return nil, xerrors.New("email is empty")
524527
}
525528
if password == "" {
526-
return xerrors.New("password is empty")
529+
return nil, xerrors.New("password is empty")
527530
}
528531
_, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{
529532
Email: email,
@@ -532,26 +535,63 @@ func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Roo
532535
OrganizationName: "acme-corp",
533536
})
534537
if err != nil {
535-
return xerrors.Errorf("create first user: %w", err)
538+
return nil, xerrors.Errorf("create first user: %w", err)
536539
}
537540
token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{
538541
Email: email,
539542
Password: password,
540543
})
541544
if err != nil {
542-
return xerrors.Errorf("login with first user: %w", err)
545+
return nil, xerrors.Errorf("login with first user: %w", err)
543546
}
544547
client.SessionToken = token.SessionToken
545548

549+
// capture the current session and if exists recover session on server exit
550+
restorePreviousSession := func() {}
551+
oldURL, _ := cfg.URL().Read()
552+
oldSession, _ := cfg.Session().Read()
553+
if oldURL != "" && oldSession != "" {
554+
restorePreviousSession = func() {
555+
currentURL, err := cfg.URL().Read()
556+
if err != nil {
557+
logger.Error(cmd.Context(), "failed to read current session url", slog.Error(err))
558+
return
559+
}
560+
currentSession, err := cfg.Session().Read()
561+
if err != nil {
562+
logger.Error(cmd.Context(), "failed to read current session token", slog.Error(err))
563+
return
564+
}
565+
566+
// if it's changed since we wrote to it don't restore session
567+
if currentURL != client.URL.String() ||
568+
currentSession != token.SessionToken {
569+
return
570+
}
571+
572+
err = cfg.URL().Write(oldURL)
573+
if err != nil {
574+
logger.Error(cmd.Context(), "failed to recover previous session url", slog.Error(err))
575+
return
576+
}
577+
err = cfg.Session().Write(oldSession)
578+
if err != nil {
579+
logger.Error(cmd.Context(), "failed to recover previous session token", slog.Error(err))
580+
return
581+
}
582+
}
583+
}
584+
546585
err = cfg.URL().Write(client.URL.String())
547586
if err != nil {
548-
return xerrors.Errorf("write local url: %w", err)
587+
return nil, xerrors.Errorf("write local url: %w", err)
549588
}
550589
err = cfg.Session().Write(token.SessionToken)
551590
if err != nil {
552-
return xerrors.Errorf("write session token: %w", err)
591+
return nil, xerrors.Errorf("write session token: %w", err)
553592
}
554-
return nil
593+
594+
return restorePreviousSession, nil
555595
}
556596

557597
// nolint:revive

0 commit comments

Comments
 (0)