gadi
02/01/2022, 12:39 PMrp
02/01/2022, 12:40 PMfetch
/ axios
? or using getServerSideProps?gadi
02/01/2022, 12:43 PMrp
02/01/2022, 12:45 PMgetSession
in getServerSideProps
as per our docs, which would cause refreshing etc..
2) On the frontend, post sign up / in, you can get the access token payload via Session.getAccessTokenPayloadSecurely()
function, and then save that in cookies and that will be sent to nextjs. But this has several security + syncing issue.
So i would recommend going with method (1). To do that though, you would need to set cookieDomain
on the backend as shown in the link you had posted earlier.gadi
02/01/2022, 12:47 PMrp
02/01/2022, 12:50 PMgetServerSideProps
is:
js
try {
// getSession will do session verification for us
session = await Session.getSession(context.req, context.res)
} catch (err) {
if (err.type === Session.Error.TRY_REFRESH_TOKEN) {
// in this case, the session is still valid, only the access token has expired.
// The refresh token is not sent to this route as it's tied to the /api/auth/session/refresh API paths.
// So we must send a "signal" to the frontend which will then call the
// refresh API and reload the page.
return { props: { fromSupertokens: 'needs-refresh' } }
// or return {fromSupertokens: 'needs-refresh'} in case of getInitialProps
} else if (err.type === Session.Error.UNAUTHORISED) {
// user is logged out. Since this is for a protected route,
// we can simple send an empty prop object. Alternatively,
// you can pass anything else you would like here.
return { props: {} }
// or return {} in case of getInitialProps
} else {
throw err
}
}
So in the block for err.type === Session.Error.TRY_REFRESH_TOKEN
, instead of doing return { props: { fromSupertokens: 'needs-refresh' } }
, you can just do whatever else you want?rp
02/01/2022, 12:51 PMgadi
02/01/2022, 12:55 PMrp
02/01/2022, 12:55 PMrp
02/01/2022, 12:57 PMerr.type === Session.Error.TRY_REFRESH_TOKEN
, what will you do in there? Show a generic something went wrong error?
Also, if you do send back return { props: { fromSupertokens: 'needs-refresh' } }
to the frontend, the frontend will attempt to manually refresh the session which will call your API (not nextjs backend). So in a way, nextjs is not actually doing the refreshing.. it's just telling the frontend to refresh and then reload the page.gadi
02/01/2022, 12:59 PMrp
02/01/2022, 1:01 PMgadi
02/01/2022, 1:02 PMrp
02/01/2022, 1:04 PMbill92
02/01/2022, 5:35 PMrp
02/01/2022, 5:36 PMextremelf
02/01/2022, 5:37 PMrp
02/01/2022, 5:38 PMrp
02/01/2022, 5:39 PMextremelf
02/01/2022, 5:40 PMrp
02/01/2022, 5:42 PMextremelf
02/01/2022, 5:45 PMrp
02/01/2022, 5:45 PMrp
02/01/2022, 5:45 PMrp
02/01/2022, 5:46 PMrp
02/01/2022, 5:46 PMextremelf
02/01/2022, 5:46 PMrp
02/01/2022, 5:47 PMextremelf
02/01/2022, 5:47 PMrp
02/01/2022, 5:48 PM