Install the Package
composer require stormpath/laravel
Publish the config file:
php artisan vendor:publish --provider="Stormpath\Laravel\StormpathServiceProvider"
Configure .env
Add your Stormpath API key (from the Stormpath Console):
STORMPATH_CLIENT_API_KEY_FILE=/path/to/your/keyfile
STORMPATH_CLIENT_APPLICATION_HREF=https://api.stormpath.com/v1/applications/YOUR_APP_HREF
First Use Case: Authenticate a User
Use the auth helper or facade to log in a user:
use Stormpath\Laravel\Facades\Stormpath;
// Login with email/password
$user = Stormpath::auth()->login('user@example.com', 'password123');
// Or via Stormpath's built-in auth middleware
Route::get('/dashboard', 'DashboardController@index')->middleware('stormpath.auth');
Verify Installation Check if the Stormpath service is registered:
php artisan tinker
>>> \Stormpath\Laravel\Facades\Stormpath::getClient();
Authentication
// Login
$user = Stormpath::auth()->login($email, $password);
// Logout
Stormpath::auth()->logout();
stormpath.auth to protect routes:
Route::group(['middleware' => 'stormpath.auth'], function () {
// Protected routes
});
$user = Stormpath::auth()->login($email, $password, true); // true = remember
User Management
$user = Stormpath::users()->create([
'email' => 'user@example.com',
'password' => 'password123',
'givenName' => 'John',
'surname' => 'Doe',
]);
$user = Stormpath::users()->getByEmail('user@example.com');
$users = Stormpath::users()->getAll();
$user->setPassword('newPassword123')->save();
Groups and Roles
$group = Stormpath::groups()->getByName('Admins');
$user->groups()->add($group);
if ($user->isInGroup('Admins')) {
// Grant admin privileges
}
Password Reset
Stormpath::passwordResets()->sendResetEmail('user@example.com');
$token = request('token');
$newPassword = request('password');
Stormpath::passwordResets()->reset($token, $newPassword);
Integration with Laravel Auth
$stormpathUser = Stormpath::auth()->getUser();
$laravelUser = \Auth::user(); // If using StormpathUser model
Custom User Model
Extend Laravel's User model to include Stormpath data:
class User extends \Illuminate\Foundation\Auth\User implements \Stormpath\Laravel\StormpathUserContract
{
public function getStormpathAccount()
{
return Stormpath::users()->getByEmail($this->email);
}
}
Stormpath Events Listen to Stormpath events (e.g., login, registration):
Stormpath::events()->listen('account.created', function ($account) {
// Send welcome email
});
Custom Claims Attach custom attributes to users:
$user->customData()->set('premium', true)->save();
Social Logins Configure Stormpath social providers (e.g., Google, Facebook) via the Stormpath Console and use:
Stormpath::auth()->loginWithSocial('google', $token);
Multi-Tenancy Use Stormpath's tenant features to manage multiple applications:
Stormpath::setApplication('https://api.stormpath.com/v1/applications/NEW_APP_HREF');
API Key File Permissions
600):
chmod 600 /path/to/your/keyfile
Invalid API key file may appear if permissions are too open.Application HREF Mismatch
STORMPATH_CLIENT_APPLICATION_HREF in .env matches your Stormpath app.Application not found or Unauthorized errors often stem from incorrect HREFs.Caching Issues
php artisan cache:clear
php artisan config:clear
Deprecated Methods
Stormpath::auth()->attempt()) may not work as expected. Prefer:
Stormpath::auth()->login($email, $password);
Timeouts and Rate Limits
Stormpath\Exception\RateLimitExceededException gracefully:
try {
$user = Stormpath::users()->getByEmail('user@example.com');
} catch (\Stormpath\Exception\RateLimitExceededException $e) {
// Retry or notify admin
}
Laravel 5.5+ Compatibility
// Example: Override the service provider
Stormpath::extend(function ($app) {
return new \Stormpath\Laravel\StormpathServiceProvider($app);
});
Enable Stormpath Debugging
Add to .env:
STORMPATH_DEBUG=true
Logs will appear in storage/logs/laravel.log.
Check HTTP Requests
Use Laravel's debug bar or dd() to inspect Stormpath responses:
$response = Stormpath::getClient()->getAccountStore()->getAccount($accountHref);
dd($response->getRawContent());
Stormpath Console Logs Monitor activity in the Stormpath Console under Logs.
Common HTTP Errors
Custom Auth Guards Extend Laravel's auth system to use Stormpath:
// config/auth.php
'guards' => [
'stormpath' => [
'driver' => 'session',
'provider' => 'stormpath',
],
],
'providers' => [
'stormpath' => [
'driver' => 'stormpath',
'model' => \App\User::class,
],
],
Stormpath Events Listen to Stormpath-specific events:
Stormpath::events()->listen('account.created', function ($account) {
// Trigger custom logic (e.g., send email)
});
Override User Model
Replace the default StormpathUser model:
// config/stormpath.php
'user_model' => \App\Models\CustomStormpathUser::class,
Custom API Clients Extend the Stormpath client for additional functionality:
Stormpath::extendClient(function ($client) {
$client->setCustomHeader('X-Custom-Header', 'value');
return $client;
});
Webhook Integration Use Stormpath webhooks to trigger Laravel jobs:
Route::post('/stormpath-webhook', function () {
$payload = request()->all();
// Process webhook (e.g., user.created)
dispatch(new HandleStormpathWebhook($payload));
});
How can I help you explore Laravel packages today?