chat icon indicating copy to clipboard operation
chat copied to clipboard

auto renew token

Open swuecho opened this issue 1 year ago • 2 comments

swuecho avatar Apr 28 '23 15:04 swuecho

https://swuecho-chat-m.fly.dev/static/#/snapshot/77adc5e2-0f13-4182-9542-2d284d1f1444

swuecho avatar May 01 '23 09:05 swuecho

system 2023-04-25T14:35:00Z: I want you to act as a software developer. I will provide some specific information about a web app requirements, and it will be your job to come up with an architecture and code for developing secure app with golang(mux as router, http for writing handler) and Vue 3. It is required that Vue 3 with style


user 2023-05-01T09:34:31Z: auto renew jwt token


assistant 2023-05-01T09:34:44Z: To handle auto renewing JWT tokens in this stack, I would:

On the backend (Go):

  • Issue JWT tokens with an expiration time (say 15 minutes)
  • Have an endpoint like /refresh_token that accepts a valid JWT and issues a new refreshed token
  • In that endpoint, validate the incoming token. If valid, issue a new token with a refreshed expiration time.

On the frontend (Vue):

  • When the app first loads, get a JWT token from the /login or /register endpoint
  • Set that token in localStorage
  • On app startup, check if there's a token in localStorage. If so, try to refresh it by calling the /refresh_token API endpoint.
  • If the refresh is successful, update the token in localStorage.
  • Set up an interval (maybe 5 minutes) to keep calling /refresh_token to auto renew the token.
  • On each API call from the frontend, include the JWT token from localStorage in the Authorization header.
  • If a refresh ever fails, redirect the user to the login page to get a new token.

The basic logic would be:

<script setup lang="ts"> 
let token = localStorage.getItem('token')
let interval: NodeJS.Timer 

onMounted(() => {
  if (token) {
    axios.get('/refresh_token', { headers: { Authorization: token }})
      .then(res => {
        localStorage.setItem('token', res.data.token)
      })
      .catch(() => {
        localStorage.removeItem('token')
        window.location = '/login'
      })
  }
  
  interval = setInterval(() => {
    axios.get('/refresh_token', { headers: { Authorization: token }})
      .then(res => {
        localStorage.setItem('token', res.data.token)
      })
      .catch(() => {
        localStorage.removeItem('token')
        window.location = '/login'
      })
  }, 1000 * 60 * 5) // 5 minutes 
}
</script> 

This will ensure the user's JWT access token is refreshed automatically in the background and stays valid. Let me know if you have any other questions!


user 2023-05-01T09:36:38Z: implement refresh_token in golang


assistant 2023-05-01T09:36:46Z: Here is how you can implement refresh tokens in Golang:

  1. Generate a refresh token when the user logs in:
refreshToken := randString(30) // Generate a random 30 char string
  1. Store the refresh token in your database along with the user ID.

  2. When the access token expires, the user will send the refresh token in a request:

router.HandleFunc("/refresh", func(w http.ResponseWriter, r *http.Request) {
  refreshToken := r.FormValue("refresh_token")
  // Query your DB to get the user ID for the given refresh token
  
  newAccessToken := generateAccessToken(userID)  // Generate a new access token
  w.Write([]byte(newAccessToken))                // Return the new access token
})
  1. In the /refresh endpoint, you query your database to get the user ID for the given refresh token.

  2. Generate a new access token for that user ID.

  3. Return the new access token in the response.

  4. The client app will get the new access token and use it to make API requests again.

  5. You should also implement logic to revoke refresh tokens when users log out to prevent unauthorized access.

This is a basic overview of how to implement refresh tokens in a Golang API. Let me know if you have any other questions!

swuecho avatar May 01 '23 09:05 swuecho

https://chat.bestqa.net/static/#/snapshot/050f714c-ee3e-48a4-9be1-3e040e2313bc

swuecho avatar Jul 17 '24 03:07 swuecho