mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user