PHPackages                             lbausch/flarum-laravel-session - 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. lbausch/flarum-laravel-session

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

lbausch/flarum-laravel-session
==============================

Use the session of Flarum within Laravel.

0.5.0(2y ago)6273MITPHPPHP ^8.1CI failing

Since Jun 25Pushed 2y ago2 watchersCompare

[ Source](https://github.com/lbausch/flarum-laravel-session)[ Packagist](https://packagist.org/packages/lbausch/flarum-laravel-session)[ RSS](/packages/lbausch-flarum-laravel-session/feed)WikiDiscussions master Synced yesterday

READMEChangelog (5)Dependencies (7)Versions (8)Used By (0)

Flarum Laravel Session
=======================

[](#flarum-laravel-session-)

[![tests](https://github.com/lbausch/flarum-laravel-session/workflows/tests/badge.svg)](https://github.com/lbausch/flarum-laravel-session/workflows/tests/badge.svg) [![codecov](https://camo.githubusercontent.com/014313c6eea848986537a8a8a1c25cec98d9910b07041da7ad88765d25b54189/68747470733a2f2f636f6465636f762e696f2f67682f6c6261757363682f666c6172756d2d6c61726176656c2d73657373696f6e2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/lbausch/flarum-laravel-session)

- [What It Does](#what-it-does)
- [Requirements](#requirements)
- [Installation and Configuration](#installation-and-configuration)
    - [Composer](#composer)
    - [Register Middleware](#register-middleware)
    - [Setup Database Connection](#setup-database-connection)
    - [Publish Package Configuration](#publish-package-configuration)
    - [Disable Cookie Encryption](#disable-cookie-encryption)
- [Usage](#usage)
    - [Setup Middleware](#setup-middleware)
    - [Handle An Identified User](#handle-an-identified-user)
- [Accessing Flarum Session Cookie From Different Domain](#accessing-flarum-session-cookie-from-different-domain)

What It Does
------------

[](#what-it-does)

This package allows to use the session of [Flarum](https://flarum.org/) for authentication within a Laravel application. It accesses Flarum's session cookie and reads the session data from the session storage. Based on the user information in the Flarum user database an user in the Laravel application is created / updated and logged in.

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 10
- Working installation of Flarum in the same filesystem as the Laravel application, so Flarum's session files can be read
- Flarum and Laravel need to share the same domain / subdomain, so Flarum's session cookie can be accessed

Installation and Configuration
------------------------------

[](#installation-and-configuration)

### Composer

[](#composer)

Install the package with Composer:

```
composer require lbausch/flarum-laravel-session
```

### Register Middleware

[](#register-middleware)

Register the `\Bausch\FlarumLaravelSession\FlarumSessionMiddleware` middleware in `app/Http/Kernel.php`:

```
/**
 * The application's middleware aliases.
 *
 * Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
 *
 * @var array
 */
protected $middlewareAliases = [
    // ...
    'flarum' => \Bausch\FlarumLaravelSession\FlarumSessionMiddleware::class,
    // ...
];
```

### Setup Database Connection

[](#setup-database-connection)

Define a database connection for the Flarum database in `config/database.php`:

```
'flarum' => [
    'driver' => 'mysql',
    'url' => env('FLARUM_DATABASE_URL'),
    'host' => env('FLARUM_DB_HOST', '127.0.0.1'),
    'port' => env('FLARUM_DB_PORT', '3306'),
    'database' => env('FLARUM_DB_DATABASE', 'forge'),
    'username' => env('FLARUM_DB_USERNAME', 'forge'),
    'password' => env('FLARUM_DB_PASSWORD', ''),
    'unix_socket' => env('FLARUM_DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('FLARUM_MYSQL_ATTR_SSL_CA'),
    ]) : [],
],
```

### Publish Package Configuration

[](#publish-package-configuration)

Publish the package configuration using `php artisan vendor:publish --provider=Bausch\\FlarumLaravelSession\\ServiceProvider` and update `config/flarum.php` with your settings.

### Disable Cookie Encryption

[](#disable-cookie-encryption)

To avoid Laravel from trying to encrypt the Flarum session cookie, add the following to `app/Http/Middleware/EncryptCookies.php`:

```
/**
 * The names of the cookies that should not be encrypted.
 *
 * @var array
 */
protected $except = [
    'flarum_session',
];
```

Usage
-----

[](#usage)

### Setup Middleware

[](#setup-middleware)

In `routes/web.php` you may assign the middleware as desired:

```
Route::middleware(['flarum'])->group(function () {
    Route::get('/', function () {
        return view('welcome');
    });
});
```

All requests to the `/` route will then be checked by the middleware.

### Handle An Identified User

[](#handle-an-identified-user)

Once the middleware successfully identified an user, it executes the default handler `\Bausch\FlarumLaravelSession\Actions\HandleIdentifiedUser`. You may configure a different handler by calling `FlarumLaravelSession::handleIdentifiedUser()` in a service provider. This is a perfect place to update attributes or execute further actions, just remember to implement the `\Bausch\FlarumLaravelSession\Contracts\FlarumUserIdentified` interface. Have a look at the default handler for a reference implementation.

```
