PHPackages                             nickmoline/stytch-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. nickmoline/stytch-laravel

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

nickmoline/stytch-laravel
=========================

Stytch integration for Laravel

v0.9.2(11mo ago)02.0k↑235.7%MITPHP

Since Jul 4Pushed 11mo agoCompare

[ Source](https://github.com/nickmoline/stytch-laravel)[ Packagist](https://packagist.org/packages/nickmoline/stytch-laravel)[ RSS](/packages/nickmoline-stytch-laravel/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (3)Dependencies (8)Versions (4)Used By (0)

Stytch Laravel Package
======================

[](#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
--------

[](#features)

- **B2C Authentication**: Full support for Stytch's B2C authentication flow
- **B2B Authentication**: Complete B2B authentication with organization support
- **Custom User Providers**: `StytchB2CUserServiceProvider` and `StytchB2BUserServiceProvider` for password authentication
- **Custom Guards**: `StytchGuard` implementing Laravel's `StatefulGuard` interface
- **Middleware**: `StytchAuthenticate` middleware for automatic authentication
- **Traits**: `HasStytchUser`, `HasStytchOrganization`, and `StytchAuthenticatable` traits
- **Session Management**: Automatic session handling with Stytch tokens
- **Organization Support**: Built-in organization management for B2B applications

Installation
------------

[](#installation)

1. Install the package via Composer:

```
composer require nickmoline/stytch-laravel
```

2. Publish the configuration file:

```
php artisan vendor:publish --provider="LaravelStytch\StytchServiceProvider"
```

3. Add your Stytch credentials to your `.env` file:

```
STYTCH_PROJECT_ID=your-project-id
STYTCH_SECRET=your-secret-key
STYTCH_ENV=test  # or 'live'
```

4. Run the migration to add the Stytch user ID column:

```
php artisan migrate
```

Configuration
-------------

[](#configuration)

### Basic Configuration

[](#basic-configuration)

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',
    ],
];
```

### Authentication Configuration

[](#authentication-configuration)

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',
    ],
],
```

Usage
-----

[](#usage)

### User Models

[](#user-models)

Your user model should implement the `Authenticatable` contract, the `StytchUserContract`, and use the provided traits:

```
