Skip to content

Commit

Permalink
Refresh token only when the old one is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
gzgsck authored Jul 22, 2020
1 parent f641268 commit 897f95a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (c *CognitoAuth) getTokenFromFile() *CognitoToken {
if err != nil {
return nil
}
defer file.Close()

b, err := ioutil.ReadAll(file)
if err != nil {
Expand All @@ -155,8 +156,7 @@ func (c *CognitoAuth) getTokenFromFile() *CognitoToken {
}
token.auth = c

//Refresh token 2 minutes before expiration
if token.ExpirationDate.Before(time.Now().Add(2 * time.Minute)) {
if token.ExpirationDate.Before(time.Now()) {
os.Remove(fileName)
return nil
}
Expand All @@ -171,6 +171,7 @@ func (c *CognitoAuth) saveToken(token CognitoToken) {
log.Warn("Failed to open or create file token", err.Error())
return
}
defer file.Close()

data, _ := json.Marshal(token)
err = file.Truncate(0)
Expand All @@ -183,7 +184,6 @@ func (c *CognitoAuth) saveToken(token CognitoToken) {
log.Warn("Failed to write to file token", err.Error())
return
}
file.Close()
}

func (c *CognitoAuth) getTokenPath() string {
Expand Down
2 changes: 1 addition & 1 deletion auth/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var templateBody = `
</head>
<body>
<h1>Login process complete, you can now close the window</h1>
<h1>Login process complete, you can now close this window</h1>
<div id="app"></div>
<script src="./cognitohosteduilauncher.js"></script>
</body>
Expand Down

0 comments on commit 897f95a

Please sign in to comment.