Hi, I'm kind of lost when trying to get session in...
# support-questions-legacy
d
Hi, I'm kind of lost when trying to get session information in
getServerSideProps
with NextJS, my calls to
SessionNode.getSession(req, res)
end up with an error that says
Copy code
SessionError: Session does not exist. Are you sending the session tokens in the request with the appropriate token transfer method?
I guess something may be wrong with my config. I am not sure whether `config.supertokens.connectionURI`should be an url that points to a supertokens core, or a supertokens backend, but either gives the same result. When looking at my developper console, I see the cookies being sent to my nextjs app. Any advice on how to solve this ?
r
hey @demisel does the sAccessToken cookie get sent to the browser navigation request (that triggers the SSR)?
d
Yes, I'm seeing it along with
sFrontToken
&
st-last-access-token-update
in all requests between the browser and my nextjs app
r
right. Can you enable backend debug logs and then try this session verification?
d
Copy code
com.supertokens {t: "2023-05-16T15:27:50.044Z", message: "getSession: Started", file: "/[...]/backend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:54:18" sdkVer: "14.0.0"} +0ms

  com.supertokens {t: "2023-05-16T15:27:50.045Z", message: "getSession: Wrapping done", file: "/[...]/backend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:62:18" sdkVer: "14.0.0"} +0ms

  com.supertokens {t: "2023-05-16T15:27:50.045Z", message: "getSession: optional validation: false", file: "/[...]/-backend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:72:18" sdkVer: "14.0.0"} +0ms

  com.supertokens {t: "2023-05-16T15:27:50.046Z", message: "getSession: UNAUTHORISED because accessToken in request is undefined", file: "/[...]/backend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:120:22" sdkVer: "14.0.0"} +0ms
That last one looks promising
r
okay. So for some reason, the function is not able to get the access token from the request object
can is see all the requests headers being sent by the browser?
d
Copy code
GET /_next/static/development/_devMiddlewareManifest.json HTTP/1.1
Host: localtest.me:7331
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0
Accept: */*
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://localtest.me:7331/fr
rid: anti-csrf
st-auth-mode: cookie
Connection: keep-alive
Cookie: st-last-access-token-update=1684249589655; sFrontToken=eyJ1aWQiOiI2MjEzYzJhNS0yNzFjLTRkNjYtYTE0Yy04ZWUyYTcyYTQ0YjAiLCJhdGUiOjE2ODQyNTE0ODkwMDAsInVwIjp7InN1YiI6IjYyMTNjMmE1LTI3MWMtNGQ2Ni1hMTRjLThlZTJhNzJhNDRiMCIsImV4cCI6MTY4NDI1MTQ4OSwiaWF0IjoxNjg0MjQ5NTg5LCJzZXNzaW9uSGFuZGxlIjoiOGE4ZjU1YzAtZGY2Ni00YWE1LTk5ZTItMDlkZWZlMzViOWNhIiwicmVmcmVzaFRva2VuSGFzaDEiOiI4MGJiY2UwZjgxNTUwMTdiYjk2NGRmOWFiNTU4NDgyOGJhZDFhMDFiYzJmZGEwODM5MDI2NzRiNGMxYTYyYTc0IiwicGFyZW50UmVmcmVzaFRva2VuSGFzaDEiOm51bGwsImFudGlDc3JmVG9rZW4iOm51bGwsImlzcyI6Imh0dHA6Ly9hcGkubG9jYWx0ZXN0Lm1lOjEzMzcvYXV0aCIsInN0LWV2Ijp7InYiOnRydWUsInQiOjE2ODQyNDk1ODk2MzB9fX0=; sAccessToken=eyJraWQiOiJkLTE2ODQxNDI1NTc1OTEiLCJ0eXAiOiJKV1QiLCJ2ZXJzaW9uIjoiMyIsImFsZyI6IlJTMjU2In0.eyJzdWIiOiI2MjEzYzJhNS0yNzFjLTRkNjYtYTE0Yy04ZWUyYTcyYTQ0YjAiLCJleHAiOjE2ODQyNTE0ODksImlhdCI6MTY4NDI0OTU4OSwic2Vzc2lvbkhhbmRsZSI6IjhhOGY1NWMwLWRmNjYtNGFhNS05OWUyLTA5ZGVmZTM1YjljYSIsInJlZnJlc2hUb2tlbkhhc2gxIjoiODBiYmNlMGY4MTU1MDE3YmI5NjRkZjlhYjU1ODQ4MjhiYWQxYTAxYmMyZmRhMDgzOTAyNjc0YjRjMWE2MmE3NCIsInBhcmVudFJlZnJlc2hUb2tlbkhhc2gxIjpudWxsLCJhbnRpQ3NyZlRva2VuIjpudWxsLCJpc3MiOiJodHRwOi8vYXBpLmxvY2FsdGVzdC5tZToxMzM3L2F1dGgiLCJzdC1ldiI6eyJ2Ijp0cnVlLCJ0IjoxNjg0MjQ5NTg5NjMwfX0.pK4IAS1cyPVf9tJ5xXbDYOUIZcVYDfxAIdTDVLgb5YRlrMcL1lN_s2v5yAUkPwxlF4ysBYesVynXeYc2hjbIMICDXWP4HeOQezDieer4LaeAIM-Nq57o3StmsrBVdaVlsa_QClfUlRSZ8vtFaETMKQblXUKSA0lANu1QGlfHjrBgxddYGbwYra1_fCST9KQ9GH2oFd6Ma0w-NYcEywpN-waPt_lX6qy4kpr7_2JBxJrsB60XS4Y53QfmxBfBzOKB8hgkXCiOPtWiutkX_0uucRc7ja4nrfMp4q0eLEgi93BC-jP46GqTlr6lB6WhyYesKTg0CIV1hvq10mCHljS-Yg
Pragma: no-cache
Cache-Control: no-cache
r
so we do have
sAccessToken
which version of the backend sdk are you using?
d
"supertokens-node": "^14.0.0",
, In the frontend :
Copy code
"supertokens-auth-react": "^0.31.3",
"supertokens-node": "^13.1.5",
"supertokens-web-js": "^0.5.0",
r
also, which version of nextjs are you using? And are you using the app directory or the pages directory?
d
I'm not using the app directory, and I do use the pages directory.
"next": "^13.2.4",
r
i see. This is really strange.
d
I agree. I also see the headers in the `req`object I am passing to
SessionNode.getSession
r
oh.. one sec
do you have another backend where you also use our superotkens backend SDK?
d
I think so ? My nextjs app is at
localtest.me:7331
, on which I'm trying to get ssr to work And I have a backend at
localtest.me:1337
, that I use to login and fetch data
r
right and that other one is a node backend?
d
Yes, express if'm not mistaken
r
ok which version of the supertokens-node sdk do yo use there?
d
The first one here is the other backend, so 14.0.0 In the nextjs app i am in 13.1.5 I Should probably update ?
r
yea.
please update the nextjs one to be 14.0.0 as well and try again
d
I'm getting cors errors, I'll get back to you when they're fixed 🙂
r
cool.
d
Getting this one now :
Copy code
SessionError: Failed to verify access token
    at Object.<anonymous> (/[...]/frontend/node_modules/supertokens-node/lib/build/recipe/session/accessToken.js:214:19)
    at Generator.throw (<anonymous>)
    at rejected (/[...]/frontend/node_modules/supertokens-node/lib/build/recipe/session/accessToken.js:72:44)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: 'TRY_REFRESH_TOKEN',
  payload: undefined,
  errMagic: 'ndskajfasndlfkj435234krjdsa',
  fromRecipe: 'session'
}
I guess that's ok and it's up to me to manage this case now
r
yea. this means the access token has expired
see our docs for how to handle this in case of ssr (in the next js section)
d
Thanks for your help, have a great day
Hey, I'm back. I return
needs-refresh
as pageProp if needed, but end up with an infinite loop of attempts to refresh. Using this in my _app.tsx :
Copy code
typescript
useEffect(() => {
    async function doRefresh() {
      if (pageProps.fromSupertokens === "needs-refresh") {
        if (await Session.attemptRefreshingSession()) {
          location.reload();
        } else {
          null;
        }
      }
    }
    doRefresh();
  }, [pageProps.fromSupertokens]);

  if (pageProps.fromSupertokens === "needs-refresh") {
    return null;
  }
It seems like
await SessionNode.getSession(req, res);
keeps answering TRY_REFRESH_TOKEN. I had set a short lifetime for sessions in my supertokens core docker-compose, but have removed it since. Am I missing something ?
r
can you enable backend debug logs and show the logs for when getSession is called?
d
They are all showing success. Hard to tell apart those coming from the browser and those from ssr
r
> those coming from the browser and those from ssr From the browser?
you can enable backend debug logs only for the nextjs process
this way only the logs for getSession from nextjs SSR will be printed
d
Oh right my bad
Copy code
2023-05-16T16:22:18.177Z com.supertokens {t: "2023-05-16T16:22:18.176Z", message: "getSession: Started", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:54:18" sdkVer: "14.0.2"}

2023-05-16T16:22:18.177Z com.supertokens {t: "2023-05-16T16:22:18.177Z", message: "getSession: Wrapping done", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:62:18" sdkVer: "14.0.2"}

2023-05-16T16:22:18.178Z com.supertokens {t: "2023-05-16T16:22:18.178Z", message: "getSession: optional validation: false", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:72:18" sdkVer: "14.0.2"}
2023-05-16T16:22:18.179Z com.supertokens {t: "2023-05-16T16:22:18.179Z", message: "getSession: got access token from cookie", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:81:30" sdkVer: "14.0.2"}
2023-05-16T16:22:18.181Z com.supertokens {t: "2023-05-16T16:22:18.180Z", message: "getSession: using cookie transfer method", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:108:22" sdkVer: "14.0.2"}

2023-05-16T16:22:18.182Z com.supertokens {t: "2023-05-16T16:22:18.181Z", message: "getSession: Value of doAntiCsrfCheck is: false", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/sessionRequestFunctions.js:136:18" sdkVer: "14.0.2"}
2023-05-16T16:22:18.183Z com.supertokens {t: "2023-05-16T16:22:18.182Z", message: "getSession: Started", file: "/frontend/node_modules/supertokens-node/lib/build/recipe/session/recipeImplementation.js:170:26" sdkVer: "14.0.2"}
SessionError: Failed to verify access token
    at Object.<anonymous> (/frontend/node_modules/supertokens-node/lib/build/recipe/session/accessToken.js:214:19)
...{
  type: 'TRY_REFRESH_TOKEN',
  payload: undefined,
  errMagic: 'ndskajfasndlfkj435234krjdsa',
  fromRecipe: 'session'
}
r
hmm. Just a failure error.
the same access token works when calling the API on the express app?
d
Yes
Wait
r
can you go to
/frontend/node_modules/supertokens-node/lib/build/recipe/session/accessToken.js:214
file and then add a print statement for printing out the
err
? (here: https://github.com/supertokens/supertokens-node/blob/master/lib/build/recipe/session/accessToken.js#L214)
d
My bad, it's actually working, I got lost among too many console.logs
r
oh
okay!
d
Thanks again, sorry for this 👼
r
no worries1
so the refreshing works?
d
Yes. There was one confusing thing though, I think what solved it was setting the appropriate url in the config "supertokens.connectionURI" field. I had left the url to my other backend, when I should have put the url to a supertokens core, right ? With the url to my other backend, I get "SessionNode.Error.TRY_REFRESH_TOKEN", which does not seem like the actual issue happening
r
right yea. You need to set it to the core
172 Views