fix: update token refresh method
This commit is contained in:
		
							parent
							
								
									f22273e351
								
							
						
					
					
						commit
						db007c3c6c
					
				@ -31,6 +31,7 @@ class SunoApi {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  private readonly client: AxiosInstance;
 | 
					  private readonly client: AxiosInstance;
 | 
				
			||||||
  private sid?: string;
 | 
					  private sid?: string;
 | 
				
			||||||
 | 
					  private currentToken?: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor(cookie: string) {
 | 
					  constructor(cookie: string) {
 | 
				
			||||||
    const cookieJar = new CookieJar();
 | 
					    const cookieJar = new CookieJar();
 | 
				
			||||||
@ -43,6 +44,12 @@ class SunoApi {
 | 
				
			|||||||
        'Cookie': cookie
 | 
					        'Cookie': cookie
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }))
 | 
					    }))
 | 
				
			||||||
 | 
					    this.client.interceptors.request.use((config) => {
 | 
				
			||||||
 | 
					      if (this.currentToken) { // 使用当前token的状态
 | 
				
			||||||
 | 
					        config.headers['Authorization'] = `Bearer ${this.currentToken}`;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return config;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async init(): Promise<SunoApi> {
 | 
					  public async init(): Promise<SunoApi> {
 | 
				
			||||||
@ -78,11 +85,9 @@ class SunoApi {
 | 
				
			|||||||
      await sleep(1, 2);
 | 
					      await sleep(1, 2);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const newToken = renewResponse.data['jwt'];
 | 
					    const newToken = renewResponse.data['jwt'];
 | 
				
			||||||
 | 
					    console.log("newToken:===\n\n", newToken);
 | 
				
			||||||
    // 更新请求头中的Authorization字段,使用新的JWT令牌
 | 
					    // 更新请求头中的Authorization字段,使用新的JWT令牌
 | 
				
			||||||
    this.client.interceptors.request.use(function (config) {
 | 
					    this.currentToken = newToken;
 | 
				
			||||||
      config.headers['Authorization'] = `Bearer ${newToken}`
 | 
					 | 
				
			||||||
      return config;
 | 
					 | 
				
			||||||
    })
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public async generate(
 | 
					  public async generate(
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user