Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react
Handle Event Hook
This function is called for various user actions. It can be used for logging, analytics or any side effect purposes (these are essentially fire and forget events).
- ReactJS
- Angular
- Vue
import Session from "supertokens-auth-react/recipe/session";
Session.init({
onHandleEvent: (context) => {
if (context.action === "SIGN_OUT") {
// called when the user clicks on sign out
} else if (context.action === "REFRESH_SESSION") {
// called with refreshing a session
// NOTE: This is an undeterministic event
} else if (context.action === "UNAUTHORISED") {
// called when the user doesn't have a valid session but made a request that requires one
// NOTE: This event can fire multiple times
if (context.sessionExpiredOrRevoked) {
// the sessionExpiredOrRevoked property is set to true if the current call cleared the session from storage
// this happens only once, even if multiple tabs sharing the same session are open, making it useful for analytics purposes
}
} else if (context.action === "SESSION_CREATED") {
// Called when session is created - post login / sign up.
} else if (context.action === "ACCESS_TOKEN_PAYLOAD_UPDATED") {
// This is called when the access token payload has been updated
}
}
})
important
Session
recipe config changes need to be reflected in both supertokens-auth-react
and supertokens-web-js
configs.
/app/auth/supertokensAuthComponent.tsx
import Session from "supertokens-auth-react/recipe/session";
Session.init({
onHandleEvent: (context) => {
if (context.action === "SIGN_OUT") {
// called when the user clicks on sign out
} else if (context.action === "REFRESH_SESSION") {
// called with refreshing a session
// NOTE: This is an undeterministic event
} else if (context.action === "UNAUTHORISED") {
// called when the user doesn't have a valid session but made a request that requires one
// NOTE: This event can fire multiple times
if (context.sessionExpiredOrRevoked) {
// the sessionExpiredOrRevoked property is set to true if the current call cleared the session from storage
// this happens only once, even if multiple tabs sharing the same session are open, making it useful for analytics purposes
}
} else if (context.action === "SESSION_CREATED") {
// Called when session is created - post login / sign up.
} else if (context.action === "ACCESS_TOKEN_PAYLOAD_UPDATED") {
// This is called when the access token payload has been updated
}
}
})
/app/app.component.ts
import SuperTokens from "supertokens-web-js";
import Session from "supertokens-web-js/recipe/session";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
},
recipeList: [
Session.init({
onHandleEvent: (context) => {
if (context.action === "SIGN_OUT") {
// called when the user clicks on sign out
} else if (context.action === "REFRESH_SESSION") {
// called with refreshing a session
// NOTE: This is an undeterministic event
} else if (context.action === "UNAUTHORISED") {
// called when the user doesn't have a valid session but made a request that requires one
// NOTE: This event can fire multiple times
if (context.sessionExpiredOrRevoked) {
// the sessionExpiredOrRevoked property is set to true if the current call cleared the session from storage
// this happens only once, even if multiple tabs sharing the same session are open, making it useful for analytics purposes
}
} else if (context.action === "SESSION_CREATED") {
// Called when session is created - post login / sign up.
} else if (context.action === "ACCESS_TOKEN_PAYLOAD_UPDATED") {
// This is called when the access token payload has been updated
}
}
}),
],
})
important
Session
recipe config changes need to be reflected in both supertokens-auth-react
and supertokens-web-js
configs.
/components/Supertokens.tsx
import Session from "supertokens-auth-react/recipe/session";
Session.init({
onHandleEvent: (context) => {
if (context.action === "SIGN_OUT") {
// called when the user clicks on sign out
} else if (context.action === "REFRESH_SESSION") {
// called with refreshing a session
// NOTE: This is an undeterministic event
} else if (context.action === "UNAUTHORISED") {
// called when the user doesn't have a valid session but made a request that requires one
// NOTE: This event can fire multiple times
if (context.sessionExpiredOrRevoked) {
// the sessionExpiredOrRevoked property is set to true if the current call cleared the session from storage
// this happens only once, even if multiple tabs sharing the same session are open, making it useful for analytics purposes
}
} else if (context.action === "SESSION_CREATED") {
// Called when session is created - post login / sign up.
} else if (context.action === "ACCESS_TOKEN_PAYLOAD_UPDATED") {
// This is called when the access token payload has been updated
}
}
})
/main.ts
import SuperTokens from "supertokens-web-js";
import Session from "supertokens-web-js/recipe/session";
SuperTokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
},
recipeList: [
Session.init({
onHandleEvent: (context) => {
if (context.action === "SIGN_OUT") {
// called when the user clicks on sign out
} else if (context.action === "REFRESH_SESSION") {
// called with refreshing a session
// NOTE: This is an undeterministic event
} else if (context.action === "UNAUTHORISED") {
// called when the user doesn't have a valid session but made a request that requires one
// NOTE: This event can fire multiple times
if (context.sessionExpiredOrRevoked) {
// the sessionExpiredOrRevoked property is set to true if the current call cleared the session from storage
// this happens only once, even if multiple tabs sharing the same session are open, making it useful for analytics purposes
}
} else if (context.action === "SESSION_CREATED") {
// Called when session is created - post login / sign up.
} else if (context.action === "ACCESS_TOKEN_PAYLOAD_UPDATED") {
// This is called when the access token payload has been updated
}
}
}),
],
})