Customizing Error Handling
SuperTokens session recipie can throw the following errors:
-
- This is a generic, something went wrong error. If this is thrown, the error will be propagated to your error handler.
-
- When using the SuperTokens middleware, this error will automatically be handled - a status code indicating session expiry will be sent to the client.
- This behavior can be overridden by supplying a custom error handler when initializing the Session Recipe in your backend code.
Interface
onUnauthorised:(message: string, request: Request, response: Response, next: NextFunction): void;
- Example code
let SuperTokens = require("supertokens-node");
let Session = require("supertokens-node/recipe/session");
SuperTokens.init({
supertokens: {...},
appInfo: {...},
recipeList: [
Session.init({
errorHandlers: {
onUnauthorised?: (message, reqest, response, next) => {
// your custom code
},
}
})
]
});
-
- When using the SuperTokens middleware, this error will automatically be handled. The middleware will automatically revoke the session and send a session expired status code to the client.
- This behavior can be overridden by supplying a custom error handler when initializing the Session Recipe in your backend code.
Interface
onTokenTheftDetected:(sessionHandle: string, userId: string, request: Request, response: Response, next: NextFunction): void;
- Example Code
let SuperTokens = require("supertokens-node");
let Session = require("supertokens-node/recipe/session");
SuperTokens.init({
supertokens: {...},
appInfo: {...},
recipeList: [
Session.init({
errorHandlers: {
onTokenTheftDetected?: (sessionHandle, userId, req, res, next) => {
// your custom code
},
}
})
]
});
TRY_REFRESH_TOKEN
- This error is thrown when the access token has expired, and to maintain the session, we must call the refresh API with the refresh session.
- The refreshing happens automatically via our frontend SDK.
- This function cannot be overridden at the moment.