Refactor user onboarding and token management

- Enhanced the `Client` initialization to include `TokenStorage` and configuration parameters.
- Replaced `SaveTokenToFile` with a `Client` method for better encapsulation.
- Improved onboarding flow with project ID verification and API enablement checks.
- Refactored token saving logic to ensure proper handling of directory creation and JSON encoding.
- Removed unused file-related code in `auth.go` for improved maintainability.
This commit is contained in:
Luis Pater
2025-07-04 07:53:07 +08:00
parent 79acea5976
commit 57ead9a4bc
4 changed files with 155 additions and 74 deletions

View File

@@ -28,8 +28,8 @@ func DoLogin(cfg *config.Config, projectID string) {
log.Info("Authentication successful.")
// 3. Initialize CLI Client
cliClient := client.NewClient(httpClient)
projectID, err = cliClient.SetupUser(clientCtx, ts.Email, projectID, ts.Auto)
cliClient := client.NewClient(httpClient, &ts, cfg)
projectID, err = cliClient.SetupUser(clientCtx, ts.Email, projectID)
if err != nil {
if err.Error() == "failed to start user onboarding, need define a project id" {
log.Error("failed to start user onboarding")
@@ -53,10 +53,26 @@ func DoLogin(cfg *config.Config, projectID string) {
}
} else {
auto := ts.ProjectID == ""
ts.ProjectID = projectID
err = auth.SaveTokenToFile(&ts, cfg, auto)
cliClient.SetProjectID(projectID)
cliClient.SetIsAuto(auto)
if !cliClient.IsChecked() && !cliClient.IsAuto() {
isChecked, checkErr := cliClient.CheckCloudAPIIsEnabled()
if checkErr != nil {
log.Fatalf("failed to check cloud api is enabled: %v", checkErr)
return
}
cliClient.SetIsChecked(isChecked)
}
if !cliClient.IsChecked() && !cliClient.IsAuto() {
return
}
err = cliClient.SaveTokenToFile()
if err != nil {
log.Fatal(err)
return
}
}
}

View File

@@ -33,7 +33,7 @@ func StartService(cfg *config.Config) {
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".json") {
log.Debugf(path)
log.Debugf("Loading token from: %s", path)
f, errOpen := os.Open(path)
if errOpen != nil {
return errOpen
@@ -56,8 +56,8 @@ func StartService(cfg *config.Config) {
log.Info("Authentication successful.")
// 3. Initialize CLI Client
cliClient := client.NewClient(httpClient)
if _, err = cliClient.SetupUser(clientCtx, ts.Email, ts.ProjectID, ts.Auto); err != nil {
cliClient := client.NewClient(httpClient, &ts, cfg)
if _, err = cliClient.SetupUser(clientCtx, ts.Email, ts.ProjectID); err != nil {
if err.Error() == "failed to start user onboarding, need define a project id" {
log.Error("failed to start user onboarding")
project, errGetProjectList := cliClient.GetProjectList(clientCtx)