Best practices using gen_auth and google Oauth2 access token?

I am not very experienced with Elixir and struggling with getting my head around how to deal with storing and updating an access token using oauth2 login. Working on an (Phoenix LiveView) application that lets the user log in with a Google account and that uses calls to the Google Api ( Drive, Documents etc)
I am using gen_auth and I have tweaked it using this setup.
What I have done is to fetch the access token and stored it using Cachex using a key named after the user id. I can retrieve this token as long as I have the user id.
The problem is that I have no strategy for when this token has expired and overall the the whole strategy feel really clunky.
I need to be able to renew the token when its expired or log out the user the this happens.
Input or other better solutions is more than welcome.
This is how I get the access_token:

 case Accounts.fetch_or_create_user(user_params) do
      {:ok, user} ->
        access_token = conn.assigns.ueberauth_auth.extra.raw_info.token.access_token
        Cachex.put(:cache, "token_user_#{user.id}", access_token)
        UserAuth.log_in_user(conn, user)

      _ ->
        conn
        |> put_flash(:error, "Authentication failed")
        |> redirect(to: "/")
    end
1 Like