Install the Package
composer require stormpath/lumen
Add the service provider to bootstrap/app.php:
$app->register('Stormpath\Lumen\StormpathServiceProvider');
Configure Stormpath
Ensure .env contains:
STORMPATH_CLIENT_APIKEY_ID=your_api_key_id
STORMPATH_CLIENT_APIKEY_SECRET=your_api_key_secret
STORMPATH_CLIENT_APPLICATION_HREF=https://api.stormpath.com/v1/applications/your_app_href
First Use Case: Auth Middleware Protect a route with Stormpath’s built-in middleware:
$app->get('/protected', ['middleware' => 'stormpath.auth', function () {
return response()->json(['message' => 'Authenticated!']);
}]);
Verify Setup
Test with curl or Postman:
curl -X GET http://your-app.test/protected -H "Authorization: Stormpath <token>"
Authentication Flow
stormpath.auth middleware for route protection.Stormpath facade:
use Stormpath\Lumen\Facades\Stormpath;
$user = Stormpath::authenticate($request);
User Management
$user = Stormpath::createUser([
'email' => 'user@example.com',
'password' => 'secure123',
]);
$user = Stormpath::getUserByEmail('user@example.com');
Session Handling
$token = Stormpath::generateToken($user);
if (Stormpath::validateToken($token)) {
$user = Stormpath::getUserFromToken($token);
}
Integration with Lumen’s Router
$app->group(['middleware' => 'stormpath.auth'], function ($app) {
$app->get('/dashboard', 'DashboardController@index');
});
Environment Variables
.env variables cause silent failures.if (!config('stormpath.client.apikey_id')) {
throw new \RuntimeException('Stormpath API key not configured.');
}
Token Expiry
$refreshedToken = Stormpath::refreshToken($oldToken);
CORS Headers
Access-Control-Allow-Origin headers if not configured.$app->middleware(['cors']);
Rate Limiting
.env:
STORMPATH_CLIENT_LOG_LEVEL=debug
Custom Auth Logic
Override the default Authenticate middleware:
$app->middleware('stormpath.auth', function ($request, $next) {
// Custom validation logic
return $next($request);
});
Stormpath Client Configuration
Extend the client in bootstrap/app.php:
$app->singleton('stormpath.client', function ($app) {
$client = new \Stormpath\Client();
$client->setHttpClient(new \GuzzleHttp\Client([
'timeout' => 30,
]));
return $client;
});
Event Listeners
Subscribe to Stormpath events (e.g., user.created):
Stormpath::on('user.created', function ($event) {
// Send welcome email
});
How can I help you explore Laravel packages today?