Minimum Setup (2 mins)
1) Copy the SuperTokens config file
From your project directory, run the following command:
php artisan vendor:publish --tag=supertokens-config
This will copy the default SuperTokens config file (supertokens.php
) to your project's config
folder.
2) Register SuperTokens middleware
- Add the following in the
$routeMiddleware
array inapp/Http/Kernel.php
protected $routeMiddleware = [
// ...other middleware
'supertokens.middleware' => \SuperTokens\Http\Middleware::class
];
3) Create a refresh API
- This API will be used to get new access and refresh tokens (done automatically from our frontend SDK).
Route::middleware("supertokens.middleware")->post("/refresh", function (Request $request) {
return "";
});
4) Add error handler
- Add this at the start of the
render
function in theapp/Exceptions/Handler.php
file - By default, SuperTokens takes care of handling session errors for you. However, you can define your own logic as well.
use SuperTokens\SuperTokens;
public function render($request, Throwable $exception) {
// add below
try {
return SuperTokens::handleError($request, $exception);
} catch (\Exception $err) {
$exception = $err;
}
// add above
return parent::render($request, $exception);
}
config.yaml
5) Change SuperTokens - Set appropriate values for
cookie_domain
andrefresh_api_path
in the SuperTokens config.yaml file. - Note that Laravel prepends your routes with
"/api"
by default
6) (Optional) Specify the location of SuperTokens Service
- The config file allows you to specify the
hostname
andport
of all the running SuperTokens instances. The default islocalhost
and3567
. - You must specify at least one
hostname
andport
pair. - All other configuration can be set in the
config.yaml
file of the SuperTokens service.
// config/supertokens.php
// we want to use two instances of SuperTokens core.
return [
'hosts' => [[
'hostname' => 'localhost',
'port' => 3567
], [
'hostname' => 'example.com',
'port' => 8080
]]
];