eliasbemlo
10/04/2023, 8:59 AMcontext
function. In it, we process the incoming request, determine the identity of the caller, and fetch their info. This means in our resolvers, we can do something like
@Query(() => Todos)
async todoList(@CurrentUser() user: User) {
return this.todoService.getForUser(user)
}
The issue I'm hitting is this:
- Within the context
method, we're calling Session.getSession
- This method throws if the supplied token is invalid
- This is supposed to be picked up by the SupertokensExceptionFilter
that we've set up, according to the docs
- But, as it turns out, the context
method is executed outside the the bounds of any Nest.js exception filters.
- So any errors thrown in there, including the one from Session
, result in raw internal server errors.
- For expired tokens, this means the client SDK won't get the transformed supertokens reponse, try refresh token
, which breaks the refresh token flow.
I have two workarounds in place now, none of which I'm happy with:
- On the client, we keep track of the token expiry and refresh it manually if it's about to expire, ensuring we always have a fresh token
- On the backend, if somebody manages to still use an invalid token, we forcibly pipe the response through the SuperTokens error handler:
import { errorHandler } from 'supertokens-node/framework/express'
// ...
try {
const session = await this.supertokens.getSession(req, res)
// ...
} catch (err) {
const handler = errorHandler()
handler(err, req, res, () =>
this.logger.error(err, 'Failed to get supertokens session'),
)
This makes Nest very unhappy though, since we're sending the response too soon.
I realize this may be more of a Nest.js support question, just wanted to see if anyone's been in the same situtation.