diff options
author | William Carroll <wpcarro@gmail.com> | 2020-02-09T13·51+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-02-10T10·06+0000 |
commit | 0f914d8aa09ba30415931367043dab74d29022bb (patch) | |
tree | 7577199a6bdf0a97359ab1e00956400c11532f53 /monzo_ynab | |
parent | 248c7a24c65a34fc9cdda0492dd5d5ad2f69c810 (diff) |
Read tokens from store when server starts
Attempting to read the persisted tokens from the key-value store when the server begins. The server currently fails when those values are empty. TODO - Consider adding logic for knowing if the cached tokens are expired and prompt the user to reauthorize the client using a web browser.
Diffstat (limited to 'monzo_ynab')
-rw-r--r-- | monzo_ynab/tokens.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/monzo_ynab/tokens.go b/monzo_ynab/tokens.go index 47e991e56132..4617595bf55a 100644 --- a/monzo_ynab/tokens.go +++ b/monzo_ynab/tokens.go @@ -16,6 +16,7 @@ import ( "net/url" "os" "time" + "kv" ) //////////////////////////////////////////////////////////////////////////////// @@ -70,8 +71,6 @@ var chans = &channels{ var ( monzoClientId = os.Getenv("monzo_client_id") monzoClientSecret = os.Getenv("monzo_client_secret") - cachedAccessToken = os.Getenv("monzo_cached_access_token") - cachedRefreshToken = os.Getenv("monzo_cached_access_token") ) //////////////////////////////////////////////////////////////////////////////// @@ -124,9 +123,21 @@ func refreshTokens(refreshToken string) (string, string) { //////////////////////////////////////////////////////////////////////////////// func main() { + // Retrieve cached tokens from store. + accessToken := fmt.Sprintf("%v", kv.Get("monzoAccessToken")) + refreshToken := fmt.Sprintf("%v", kv.Get("monzoRefreshToken")) + + log.Println("Attempting to retrieve cached credentials...") + log.Printf("Access token: %s\n", accessToken) + log.Printf("Refresh token: %s\n", refreshToken) + + if accessToken == "" || refreshToken == "" { + log.Fatal("Cannot start server without access or refresh tokens.") + } + // Manage application state. go func() { - state := &state{cachedAccessToken, cachedRefreshToken} + state := &state{accessToken, refreshToken} for { select { case msg := <-chans.reads: |