EnsureAuthenticated returns a valid access token from the stored session. If no valid session exists and the terminal is interactive, it triggers the browser-based OAuth login flow automatically.
( io *iostreams.IOStreams, client *dashboard.Client, )
| 12 | // If no valid session exists and the terminal is interactive, it triggers the |
| 13 | // browser-based OAuth login flow automatically. |
| 14 | func EnsureAuthenticated( |
| 15 | io *iostreams.IOStreams, |
| 16 | client *dashboard.Client, |
| 17 | ) (string, error) { |
| 18 | accessToken, err := GetValidToken(client) |
| 19 | if err == nil { |
| 20 | return accessToken, nil |
| 21 | } |
| 22 | |
| 23 | cs := io.ColorScheme() |
| 24 | fmt.Fprintf(io.Out, "%s %s\n", cs.WarningIcon(), err) |
| 25 | |
| 26 | // No flow tracker: this re-authentication belongs to the calling flow, |
| 27 | // not to an `auth login` funnel. |
| 28 | return RunOAuth(io, client, false, true, nil) |
| 29 | } |
| 30 | |
| 31 | // ReauthenticateIfExpired checks if err is a session-expired error from the API. |
| 32 | // If so, it clears the invalid token and triggers the login flow. |
no test coverage detected