nickmoline/stytch-laravel is a Laravel package for stytch integration for laravel.
It currently has 0 GitHub stars and 1.839 downloads on Packagist (latest version v0.9.2).
Install it with composer require nickmoline/stytch-laravel.
Discover more Laravel packages by nickmoline
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel package for integrating Stytch authentication into your Laravel application. This package provides seamless integration with Stytch's B2C and B2B authentication services.
StytchB2CUserServiceProvider and StytchB2BUserServiceProvider for password authenticationStytchGuard implementing Laravel's StatefulGuard interfaceStytchAuthenticate middleware for automatic authenticationHasStytchUser, HasStytchOrganization, and StytchAuthenticatable traitscomposer require nickmoline/stytch-laravel
php artisan vendor:publish --provider="LaravelStytch\StytchServiceProvider"
.env file:STYTCH_PROJECT_ID=your-project-id
STYTCH_SECRET=your-secret-key
STYTCH_ENV=test # or 'live'
php artisan migrate
The package configuration is located in config/stytch.php. Here are the key options:
return [
'project_id' => env('STYTCH_PROJECT_ID'),
'secret' => env('STYTCH_SECRET'),
'env' => env('STYTCH_ENV', 'test'),
'user_model' => 'App\Models\User',
'stytch_user_id_column' => 'stytch_user_id',
'email_column' => 'email',
'session_cookie_name' => 'stytch_session',
'jwt_cookie_name' => 'stytch_session_jwt',
'session_timeout' => 3600, // 1 hour
'organization' => [
'enabled' => true,
'model' => 'App\Models\Organization',
'stytch_organization_id_column' => 'stytch_organization_id',
],
];
Update your config/auth.php to use the Stytch guards and providers:
'guards' => [
'web' => [
'driver' => 'stytch-b2c',
'provider' => 'stytch-b2c',
],
'b2b' => [
'driver' => 'stytch-b2b',
'provider' => 'stytch-b2b',
],
],
'providers' => [
'stytch-b2c' => [
'driver' => 'stytch-b2c',
'model' => 'App\Models\User',
],
'stytch-b2b' => [
'driver' => 'stytch-b2b',
'model' => 'App\Models\User',
],
],
Your user model should implement the Authenticatable contract, the StytchUserContract, and use the provided traits:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable;
use LaravelStytch\Contracts\StytchUserContract;
use LaravelStytch\Traits\HasStytchUser;
use LaravelStytch\Traits\StytchAuthenticatable;
class User implements Authenticatable, StytchUserContract
{
use HasStytchUser, StytchAuthenticatable;
protected $fillable = [
'name',
'email',
'stytch_user_id',
];
/**
* Update the user's name from Stytch data.
* Override this method if your name field has a different name.
*/
public function updateStytchName(string $name): void
{
$this->name = $name;
}
/**
* Get the user's current name.
* Override this method if your name field has a different name.
*/
public function getStytchName(): ?string
{
return $this->name;
}
}
The StytchUserContract provides methods for updating user data from Stytch responses without making assumptions about your specific field names. Email handling is already provided by the HasStytchUser trait, while name handling can be customized by overriding the contract methods.
The StytchGuard implements Laravel's StatefulGuard interface, providing all standard authentication methods:
// Login with credentials
Auth::attempt(['email' => $email, 'password' => $password]);
// Login a user directly
Auth::login($user);
// Login using user ID
Auth::loginUsingId($userId);
// Login without session (stateless)
Auth::once(['email' => $email, 'password' => $password]);
// Logout the current user
Auth::logout();
// Check if user is authenticated
if (Auth::check()) {
// User is logged in
}
// Get current user
$user = Auth::user();
// Get user ID
$userId = Auth::id();
For B2B applications, you can also use the HasStytchOrganization trait:
<?php
namespace App\Models;
use LaravelStytch\Traits\HasStytchOrganization;
class Organization extends Model
{
use HasStytchOrganization;
protected $fillable = [
'name',
'stytch_organization_id',
];
}
The package provides a StytchAuthenticate middleware that automatically handles authentication via Stytch cookies. This middleware:
Auth::login() methodApply the middleware to routes that require authentication:
// Apply to individual routes
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware('stytch.auth');
// Apply to route groups
Route::middleware(['stytch.auth'])->group(function () {
Route::get('/profile', 'ProfileController@show');
Route::get('/settings', 'SettingsController@show');
});
// Apply to controllers
Route::controller(ProfileController::class)
->middleware('stytch.auth')
->group(function () {
Route::get('/profile', 'show');
Route::put('/profile', 'update');
});
The middleware is registered as stytch.auth and can be used alongside other Laravel middleware.
The guard automatically handles Stytch session tokens and creates Laravel sessions. You can access Stytch session data:
// Get Stytch session data
$sessionData = Auth::guard('b2b')->getStytchSessionData();
// Clear Stytch session
Auth::guard('b2b')->clearStytchSession();
You can extend the guard or create custom authentication logic:
// Custom authentication with additional logic
$user = Auth::guard('b2c')->getProvider()->retrieveByCredentials([
'email' => $email,
'password' => $password,
]);
if ($user && Auth::guard('b2c')->getProvider()->validateCredentials($user, $credentials)) {
Auth::login($user);
// Custom logic here
}
You can use different guards for different parts of your application:
// B2C authentication
Auth::guard('web')->attempt($credentials);
// B2B authentication
Auth::guard('b2b')->attempt($credentials);
"Call to undefined method login()": Make sure you're using the latest version of the package that implements StatefulGuard.
User not found: Ensure your user model uses the HasStytchUser trait and has the correct Stytch user ID.
Session issues: Check that your session configuration is correct and that cookies are being set properly.
Enable debug mode in your .env file to see detailed error messages:
STYTCH_DEBUG=true
Please see CONTRIBUTING.md for details.
The MIT License (MIT). Please see License File for more information.