Download the PHP package nickmoline/stytch-laravel without Composer
On this page you can find all versions of the php package nickmoline/stytch-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nickmoline/stytch-laravel
More information about nickmoline/stytch-laravel
Files in nickmoline/stytch-laravel
Package stytch-laravel
Short Description Stytch integration for Laravel
License MIT
Informations about the package stytch-laravel
Stytch Laravel Package
A Laravel package for integrating Stytch authentication into your Laravel application. This package provides seamless integration with Stytch's B2C and B2B authentication services.
Features
- B2C Authentication: Full support for Stytch's B2C authentication flow
- B2B Authentication: Complete B2B authentication with organization support
- Custom User Providers:
StytchB2CUserServiceProvider
andStytchB2BUserServiceProvider
for password authentication - Custom Guards:
StytchGuard
implementing Laravel'sStatefulGuard
interface - Middleware:
StytchAuthenticate
middleware for automatic authentication - Traits:
HasStytchUser
,HasStytchOrganization
, andStytchAuthenticatable
traits - Session Management: Automatic session handling with Stytch tokens
- Organization Support: Built-in organization management for B2B applications
Installation
-
Install the package via Composer:
-
Publish the configuration file:
-
Add your Stytch credentials to your
.env
file: - Run the migration to add the Stytch user ID column:
Configuration
Basic Configuration
The package configuration is located in config/stytch.php
. Here are the key options:
Authentication Configuration
Update your config/auth.php
to use the Stytch guards and providers:
Usage
User Models
Your user model should implement the Authenticatable
contract, the StytchUserContract
, and use the provided traits:
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.
Authentication Methods
The StytchGuard
implements Laravel's StatefulGuard
interface, providing all standard authentication methods:
Login
Logout
Check Authentication
B2B Organization Support
For B2B applications, you can also use the HasStytchOrganization
trait:
Middleware Authentication
The package provides a StytchAuthenticate
middleware that automatically handles authentication via Stytch cookies. This middleware:
- Checks if the user is already logged in via Laravel sessions
- If not logged in, checks for Stytch cookies (session_token or session_jwt)
- Authenticates with Stytch using the appropriate method (B2B or B2C)
- Finds or creates the user in your database
- Logs the user in using Laravel's
Auth::login()
method
Using the Middleware
Apply the middleware to routes that require authentication:
The middleware is registered as stytch.auth
and can be used alongside other Laravel middleware.
Session Management
The guard automatically handles Stytch session tokens and creates Laravel sessions. You can access Stytch session data:
Advanced Usage
Custom Authentication Logic
You can extend the guard or create custom authentication logic:
Multiple Guards
You can use different guards for different parts of your application:
Troubleshooting
Common Issues
-
"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.
Debug Mode
Enable debug mode in your .env
file to see detailed error messages:
Security Considerations
- Always use HTTPS in production
- Regularly rotate your Stytch API keys
- Implement proper session timeout
- Use CSRF protection for all forms
- Validate and sanitize all user inputs
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.