├── .editorconfig ├── README.md ├── composer.json └── src ├── Events └── SessionDestroyed.php ├── Listeners ├── ClearLastSession.php └── StoreLastSession.php └── SingleSessionServiceProvider.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = false 10 | 11 | [*.{vue,js,scss}] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | end_of_line = lf 16 | insert_final_newline = true 17 | trim_trailing_whitespace = true 18 | 19 | [*.md] 20 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-single-session 2 | 3 | A plugin provide single session authentication for Laravel 5. 4 | 5 | ## Installing 6 | 7 | ```shell 8 | $ composer require overtrue/laravel-single-session -vvv 9 | ``` 10 | 11 | ## Usage 12 | 13 | 1. Register provider(if you disabled Laravel auto discovery) 14 | 15 | ```php 16 | 'providers' => [ 17 | Overtrue\LaravelSingleSession\SingleSessionServiceProvider::class 18 | ], 19 | ``` 20 | 21 | 2. (Optional) Config the session: 22 | 23 | ```php 24 | // config/session.php 25 | // The storage for store the last session id. 26 | 'last_session_storage' => 'cache', // cache(default)/database 27 | 28 | // The field name of last session id. 29 | 'last_session_field' => 'last_session_id', 30 | 31 | // The path of redirect when logout after session changed. 32 | 'redirect_path' => '/', 33 | ``` 34 | 35 | 3. it works. 36 | 37 | 38 | ### Events 39 | 40 | If the session changed, the following event will be triggered: 41 | 42 | ```phl 43 | Overtrue\LaravelSingleSession\Events\SessionExpired 44 | ``` 45 | 46 | ## PHP 扩展包开发 47 | 48 | > 想知道如何从零开始构建 PHP 扩展包? 49 | > 50 | > 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package) 51 | 52 | ## License 53 | 54 | MIT 55 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "overtrue/laravel-single-session", 3 | "description": "A plugin provide single session authentication for Laravel 5.", 4 | "type": "library", 5 | "require": { 6 | "laravel/framework": "~5.5 || ~6.0" 7 | }, 8 | "license": "MIT", 9 | "authors": [ 10 | { 11 | "name": "overtrue", 12 | "email": "anzhengchao@gmail.com" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4":{ 17 | "Overtrue\\LaravelSingleSession\\": "./src" 18 | } 19 | }, 20 | "extra": { 21 | "laravel": { 22 | "aliases": { 23 | "LaravelSingleSessionMiddleware": "Overtrue\\LaravelSingleSession\\Middleware\\SingleSession" 24 | }, 25 | "providers": ["Overtrue\\LaravelSingleSession\\SingleSessionServiceProvider"] 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Events/SessionDestroyed.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace Overtrue\LaravelSingleSession\Events; 13 | 14 | use Illuminate\Broadcasting\InteractsWithSockets; 15 | use Illuminate\Foundation\Events\Dispatchable; 16 | use Illuminate\Queue\SerializesModels; 17 | 18 | class SessionDestroyed 19 | { 20 | use Dispatchable, InteractsWithSockets, SerializesModels; 21 | 22 | public $user; 23 | public $sessionId; 24 | 25 | /** 26 | * Create a new event instance. 27 | */ 28 | public function __construct($user, $sessionId) 29 | { 30 | $this->user = $user; 31 | $this->sessionId = $sessionId; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Listeners/ClearLastSession.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace Overtrue\LaravelSingleSession\Listeners; 13 | 14 | use Illuminate\Support\Facades\Cache; 15 | use Illuminate\Support\Facades\Event; 16 | use Illuminate\Support\Facades\Session; 17 | use Overtrue\LaravelSingleSession\Events\SessionDestroyed; 18 | 19 | /** 20 | * Class ClearLastSession. 21 | * 22 | * @author overtrue 23 | */ 24 | class ClearLastSession 25 | { 26 | /** 27 | * Handle the event. 28 | * 29 | * @param object $event 30 | */ 31 | public function handle($event) 32 | { 33 | $lastSessionField = config('last_session_field', 'last_session_id'); 34 | 35 | if (config('session.last_session_storage', 'cache')) { 36 | $previousSessionId = Cache::pull($lastSessionField.'.'.$event->user->id); 37 | Session::getHandler()->destroy($previousSessionId); 38 | Event::dispatch(new SessionDestroyed($event->user, $previousSessionId)); 39 | } else { 40 | $event->user->update([$lastSessionField => null]); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Listeners/StoreLastSession.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace Overtrue\LaravelSingleSession\Listeners; 13 | 14 | use Illuminate\Support\Facades\Cache; 15 | use Illuminate\Support\Facades\Session; 16 | 17 | /** 18 | * Class StoreLastSession. 19 | * 20 | * @author overtrue 21 | */ 22 | class StoreLastSession 23 | { 24 | /** 25 | * Handle the event. 26 | * 27 | * @param object $event 28 | */ 29 | public function handle($event) 30 | { 31 | $currentSessionId = Session::getId(); 32 | $lastSessionField = config('last_session_field', 'last_session_id'); 33 | 34 | if (config('session.last_session_storage', 'cache')) { 35 | Cache::forever($lastSessionField.'.'.$event->user->id, $currentSessionId); 36 | } else { 37 | $event->user->update([$lastSessionField => $currentSessionId]); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/SingleSessionServiceProvider.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * This source file is subject to the MIT license that is bundled 9 | * with this source code in the file LICENSE. 10 | */ 11 | 12 | namespace Overtrue\LaravelSingleSession; 13 | 14 | use Illuminate\Support\Facades\Event; 15 | use Illuminate\Support\ServiceProvider; 16 | use Overtrue\LaravelSingleSession\Listeners\ClearLastSession; 17 | use Overtrue\LaravelSingleSession\Listeners\StoreLastSession; 18 | 19 | /** 20 | * Class SingleSessionServiceProvider. 21 | * 22 | * @author overtrue 23 | */ 24 | class SingleSessionServiceProvider extends ServiceProvider 25 | { 26 | public function boot() 27 | { 28 | Event::listen(\Illuminate\Auth\Events\Login::class, ClearLastSession::class); 29 | Event::listen(\Illuminate\Auth\Events\Authenticated::class, StoreLastSession::class); 30 | } 31 | 32 | public function register() 33 | { 34 | // do nothing 35 | } 36 | } 37 | --------------------------------------------------------------------------------