My session keeps expiring I tried adding the front...
# support-questions
λ
My session keeps expiring I tried adding the frontend SDK to all of my pages but it doesn't seem to work
r
My session keeps expiring I tried adding the frontend SDK to all of my pages but it doesn't seem to work
hey @Λ C Ξ L X R D
You do not need to manually handle session expiry. If you are using fetch for your API calls, just make sure that supertokens.init is called on all pages
if you are using axios, then make sure that all axios instances have the supertokens interceptor added to it
the session refreshing should happen on its own
λ
I called supertokens.init on all the pages and it still brings up this issue
this is the js file i am using
Copy code
js
import "https://cdn.jsdelivr.net/gh/supertokens/supertokens-website/bundle/bundle.js"

supertokens.init(
    {
        apiDomain: "http://localhost:8000",
        apiBasePath: "/auth",
});
r
and are you using fetch everywhere?
λ
why would I need to use fetch when serving html?
r
wait.. is this for server side rendered pages?
λ
yes
r
oh right.. for those routes, you shouldn't use
verifySession
function
λ
what should I use then?
it is not really clear in the guide that I should use something else
You should use
getSession
λ
is there a guide to use it in python?
r
and if that throws a
Session.Error.TRY_REFRESH_TOKEN
, then you should send an HTML to the frontend which calls the
attemptRefreshingSession
on the frontend
λ
okay i tried using getSession
Copy code
py
@router.get("/dashboard", response_class=HTMLResponse)
async def dashboard_get(request: Request):
    session = await get_session(request)
    print(session)
    if session is None:
        print("No session")
        return RedirectResponse("/")
    else:
        print("Session found")
it doesn't print any of the statements I put tho
r
What are the request headers that are being sent for this route?
λ
you want me to intercept the http request?
r
no. You can acccess the page from chrome and see the network tab
which python framework are you using?
λ
Fastapi
r
have you added supertokens' middleware in your app?
Also, can you enable debug logging (see troubleshooting section) and then show the log output when you call this route?
λ
yep
r
this please.
λ
this is the output
Copy code
INFO:     127.0.0.1:1188 - "GET /dashboard HTTP/1.1" 401 Unauthorized
com.supertokens {"t": "2022-05-04T11:20:42.978Z", "sdkVer": "0.6.5", "message": "middleware: Started", "file": "supertokens.py:339"}

com.supertokens {"t": "2022-05-04T11:20:42.978Z", "sdkVer": "0.6.5", "message": "middleware: Not handling because request path did not start with config path. Request path: /dashboard", "file": "supertokens.py:347"}

com.supertokens {"t": "2022-05-04T11:20:42.979Z", "sdkVer": "0.6.5", "message": "getSession: Started", "file": "recipe\\session\\recipe_implementation.py:134"}

com.supertokens {"t": "2022-05-04T11:20:42.979Z", "sdkVer": "0.6.5", "message": "getSession: rid in header: False", "file": "recipe\\session\\recipe_implementation.py:138"}

com.supertokens {"t": "2022-05-04T11:20:42.979Z", "sdkVer": "0.6.5", "message": "getSession: request method: GET", "file": "recipe\\session\\recipe_implementation.py:139"}

com.supertokens {"t": "2022-05-04T11:20:42.979Z", "sdkVer": "0.6.5", "message": "getSession: Returning try refresh token because access token from cookies is undefined", "file": "recipe\\session\\recipe_implementation.py:152"}

com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "errorHandler: Started", "file": "supertokens.py:393"}

com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "errorHandler: Error is from SuperTokens recipe. Message: Access token has expired. Please call the refresh API", "file": "supertokens.py:394"}

com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "errorHandler: Checking recipe for match: session", "file": "supertokens.py:403"}

com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "errorHandler: Matched with recipeID: session", "file": "supertokens.py:406"}
Copy code
com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "errorHandler: returning TRY_REFRESH_TOKEN", "file": "recipe\\session\\recipe.py:152"}

com.supertokens {"t": "2022-05-04T11:20:42.980Z", "sdkVer": "0.6.5", "message": "Sending response to client with status code: 401", "file": "utils.py:111"}
r
ok.
so this is working as expected. What you need to do is this:
Copy code
python
from supertokens_python.recipe.session.asyncio import get_session
from supertokens_python.recipe.session.exceptions import TryRefreshTokenError

async def dashboard_get(request: Request):
    try: 
      session = await get_session(request)
      print(session)
      if session is None:
          print("No session")
          return RedirectResponse("/")
      else:
          print("Session found")
    except TryRefreshTokenError:
        print("need to refresh session")
        # TODO: send back HTML to the frontend which will refresh the session
does it print out "need to refresh session"?
the html you send to the frontend in the
except
block needs to call
supertokens.init
and then run
supertokens.attemptRefreshingSession()
. If this is successful, you should simply reload the page, else you should redirect the user to the login page
λ
yes it does
r
ok great! that's expected
the html stuff to send to the frontend, does that make sense?
λ
can't I refresh the session from the backend?
r
you can't
the refresh token is stored on the frontend
and this flow will only happen when the access token expires
so not too often
λ
Okay i wrote some javascript to refresh the session
but how can I wait till the refresh session is complete?
r
can I see the JS code you wrote?
λ
Copy code
js
<script>
    supertokens.init(
        {
            apiDomain: "http://localhost:8000",
            apiBasePath: "/auth",
        }
    );
    supertokens.attemptRefreshingSession()
    console.log("Session refreshed successfully");
    window.location.href = "redirection url here"
</script>
it redirects me before refreshing the token
r
change it to:
Copy code
<script>
    supertokens.init(
        {
            apiDomain: "http://localhost:8000",
            apiBasePath: "/auth",
        }
    );
    supertokens.attemptRefreshingSession().then(success => {
      if (success) {
        // reload page
      } else {
        // redirect to login page
      }
    })
</script>
λ
Thank you it worked 🙏
r
awesome!
10 Views