mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-07 13:44:17 +08:00
retry only once for refreshing JWT
This commit is contained in:
parent
94dc0d9c8a
commit
98e60edb19
1 changed files with 5 additions and 3 deletions
|
@ -29,7 +29,7 @@ func getAuthToken(ctx config.Context, force bool) string {
|
|||
log.Fatalf("Client could not read response body: %s", err)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
log.Fatalf("Error response: %s", string(resBodyBytes))
|
||||
log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
|
||||
}
|
||||
body := new(models.SuccessResponse)
|
||||
if err := json.Unmarshal(resBodyBytes, body); err != nil {
|
||||
|
@ -67,14 +67,16 @@ func request[T any](method, route string, payload any) *T {
|
|||
} else {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, false))
|
||||
}
|
||||
retried := false
|
||||
retry:
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatalf("Client error making http request: %s", err)
|
||||
}
|
||||
// refresh JWT token
|
||||
if res.StatusCode == http.StatusUnauthorized {
|
||||
if res.StatusCode == http.StatusUnauthorized && !retried {
|
||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||
retried = true
|
||||
goto retry
|
||||
}
|
||||
resBodyBytes, err := io.ReadAll(res.Body)
|
||||
|
@ -82,7 +84,7 @@ retry:
|
|||
log.Fatalf("Client could not read response body: %s", err)
|
||||
}
|
||||
if res.StatusCode != http.StatusOK {
|
||||
log.Fatalf("Error Status: %d Response: %s", http.StatusOK, string(resBodyBytes))
|
||||
log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
|
||||
}
|
||||
body := new(T)
|
||||
if len(resBodyBytes) > 0 {
|
||||
|
|
Loading…
Add table
Reference in a new issue