about summary refs log tree commit diff
path: root/monzo_ynab
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-02-09T13·51+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-02-10T10·06+0000
commit0f914d8aa09ba30415931367043dab74d29022bb (patch)
tree7577199a6bdf0a97359ab1e00956400c11532f53 /monzo_ynab
parent248c7a24c65a34fc9cdda0492dd5d5ad2f69c810 (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.go17
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: