Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7c0bcc2

Browse files
f0sselkylecarbs
authored andcommitted
fix: restore previous session on coder server --dev (#1821)
1 parenteb410b9 commit7c0bcc2

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

‎cli/server.go

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,11 @@ func server() *cobra.Command {
336336
returnxerrors.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
iferr!=nil {
341341
returnxerrors.Errorf("create first user: %w",err)
342342
}
343+
deferrestorePreviousSession()
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
returnroot
519520
}
520521

521-
funccreateFirstUser(cmd*cobra.Command,client*codersdk.Client,cfg config.Root,email,passwordstring)error {
522+
// createFirstUser creates the first user and sets a valid session.
523+
// Caller must call restorePreviousSession on server exit.
524+
funccreateFirstUser(logger slog.Logger,cmd*cobra.Command,client*codersdk.Client,cfg config.Root,email,passwordstring) (func(),error) {
522525
ifemail=="" {
523-
returnxerrors.New("email is empty")
526+
returnnil,xerrors.New("email is empty")
524527
}
525528
ifpassword=="" {
526-
returnxerrors.New("password is empty")
529+
returnnil,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
iferr!=nil {
535-
returnxerrors.Errorf("create first user: %w",err)
538+
returnnil,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
iferr!=nil {
542-
returnxerrors.Errorf("login with first user: %w",err)
545+
returnnil,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+
ifoldURL!=""&&oldSession!="" {
554+
restorePreviousSession=func() {
555+
currentURL,err:=cfg.URL().Read()
556+
iferr!=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+
iferr!=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+
ifcurrentURL!=client.URL.String()||
568+
currentSession!=token.SessionToken {
569+
return
570+
}
571+
572+
err=cfg.URL().Write(oldURL)
573+
iferr!=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+
iferr!=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
iferr!=nil {
548-
returnxerrors.Errorf("write local url: %w",err)
587+
returnnil,xerrors.Errorf("write local url: %w",err)
549588
}
550589
err=cfg.Session().Write(token.SessionToken)
551590
iferr!=nil {
552-
returnxerrors.Errorf("write session token: %w",err)
591+
returnnil,xerrors.Errorf("write session token: %w",err)
553592
}
554-
returnnil
593+
594+
returnrestorePreviousSession,nil
555595
}
556596

557597
// nolint:revive

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp