]> git.giacomolli.uk - onechat/blobdiff - auth.go
Check the socket path before the daemon writes
[onechat] / auth.go
diff --git a/auth.go b/auth.go
index 736624e37e0850df3f21f116c60862815282f571..fda78d8ac709473f20a0a559506e0652b220e7ba 100644 (file)
--- a/auth.go
+++ b/auth.go
@@ -97,13 +97,23 @@ type deviceGrant struct {
 }
 
 // login runs "onechat login": the device code login into the token file of the
-// data directory.
-func login(ctx context.Context, out io.Writer) error {
+// data directory. It holds the lock of the token file, so it does not run while
+// the daemon runs.
+func login(ctx context.Context, out io.Writer) (err error) {
        dir, err := dataDir()
        if err != nil {
                return err
        }
 
+       tokens, err := lockTokens(dir)
+       if err != nil {
+               return err
+       }
+
+       defer func() {
+               err = errors.Join(err, tokens.Close())
+       }()
+
        return newChatGPTAuth(filepath.Join(dir, tokenFileName), authIssuer).Login(ctx, out)
 }