Session Object
A Session object is returned when a session is verified successfully.
$session = SuperTokens\SuperTokens::getSession($request, $response, $enableCsrfProtection);
Following are the functions you can use on this session object:
getUserId
function: API Reference
Call the $session->getUserId()
- This function does not do any database call.
getJWTPayload
function: API Reference
Call the session->getJWTPayload()
- This function does not do any database call.
- It reads the payload available in the JWT access token that was used to verify this session.
revokeSession
function: API Reference
Call the session->revokeSession()
- Use this to logout a user from their current session.
- This function deletes the session from the database and clears relevant auth cookies
- If using blacklisting, this will immediately invalidate the JWT access token.
getSessionData
function: API Reference
Call the session->getSessionData()
- This function requires a database call each time it's called.
updateSessionData
function: API Reference
Call the session->updateSessionData($newData)
- This function overrides the current session data stored for this session.
- This function requires a database call each time it's called.
Example
Route::post('/like-comment', function (Illuminate\Http\Request $request) {
$response = new \Illuminate\Http\Response();
$session = SuperTokens\SuperTokens::getSession($request, $response, true);
$userId = $session->getUserId();
$jwtPayloadData = $session->getJWTPayload();
//update session info example
try {
$sessionData = $session->getSessionData();
$sessionData["newKey"] = "newValue";
$session->updateSessionData($sessionData);
} catch(SuperTokensUnauthorisedException $e) {
$response->setStatusCode(440)->setContent("Please login again");
} catch(SuperTokensGeneralException $e) {
$response->setStatusCode(500)->setContent("Something went wrong");
}
try {
$session->revokeSession();
} catch(SuperTokensGeneralException $e) {
$response->setStatusCode(500)->setContent("Something went wrong");
}
return $response;
});