├── tests
└── .gitkeep
├── migrations
└── .gitkeep
├── .gitignore
├── phpunit.xml.dist
├── .github
└── workflows
│ └── ci.yml
├── src
└── Http
│ └── Middleware
│ └── SetSessionFromHeaders.php
├── LICENSE
├── .php-cs-fixer.php
├── composer.json
└── README.md
/tests/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/migrations/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 |
3 | /vendor/
4 | node_modules/
5 | npm-debug.log
6 | yarn-error.log
7 |
8 | # Laravel 4 specific
9 | bootstrap/compiled.php
10 | app/storage/
11 |
12 | # Laravel 5 & Lumen specific
13 | public/storage
14 | public/hot
15 |
16 | # Laravel 5 & Lumen specific with changed public path
17 | public_html/storage
18 | public_html/hot
19 |
20 | storage/*.key
21 | .env
22 | Homestead.yaml
23 | Homestead.json
24 | /.vagrant
25 | .phpunit.result.cache
26 |
27 | cghooks.lock
28 | .php_cs.cache
29 | .php-cs-fixer.cache
30 | composer.lock
31 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
15 | src/
16 |
17 |
18 |
19 |
20 | ./tests/
21 | vendor
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 |
10 | jobs:
11 | phpcs:
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/checkout@v2
15 | - name: Setup PHP environment
16 | uses: shivammathur/setup-php@v2
17 | - name: Install dependencies
18 | run: composer install
19 | - name: PHPCSFixer check
20 | run: composer check-style
21 | phpunit:
22 | strategy:
23 | matrix:
24 | php_version: [8.0, 8.1]
25 | runs-on: ubuntu-latest
26 | steps:
27 | - uses: actions/checkout@v2
28 | - name: Setup PHP environment
29 | uses: shivammathur/setup-php@v2
30 | with:
31 | php-version: ${{ matrix.php_version }}
32 | coverage: xdebug
33 | - name: Install dependencies
34 | run: composer install
35 | - name: PHPUnit check
36 | run: ./vendor/bin/phpunit --coverage-text
37 |
--------------------------------------------------------------------------------
/src/Http/Middleware/SetSessionFromHeaders.php:
--------------------------------------------------------------------------------
1 | header($headerName);
23 |
24 | if (empty($sessionId) || $sessionId === 'undefined') {
25 | return $next($request);
26 | }
27 |
28 | $request->cookies->add([Session::getName() => $sessionId]);
29 |
30 | return tap($next($request), fn ($response) => $response->headers->set($headerName, Session::getId()));
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2020 overtrue
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/.php-cs-fixer.php:
--------------------------------------------------------------------------------
1 | setRules([
5 | '@PSR12' => true,
6 | 'binary_operator_spaces' => true,
7 | 'blank_line_after_opening_tag' => true,
8 | 'compact_nullable_typehint' => true,
9 | 'declare_equal_normalize' => true,
10 | 'lowercase_cast' => true,
11 | 'lowercase_static_reference' => true,
12 | 'new_with_braces' => true,
13 | 'no_blank_lines_after_class_opening' => true,
14 | 'no_leading_import_slash' => true,
15 | 'no_whitespace_in_blank_line' => true,
16 | 'no_unused_imports' => true,
17 | 'ordered_class_elements' => [
18 | 'order' => [
19 | 'use_trait',
20 | ],
21 | ],
22 | 'ordered_imports' => [
23 | 'imports_order' => [
24 | 'class',
25 | 'function',
26 | 'const',
27 | ],
28 | 'sort_algorithm' => 'none',
29 | ],
30 | 'return_type_declaration' => true,
31 | 'short_scalar_cast' => true,
32 | 'single_blank_line_before_namespace' => true,
33 | 'single_trait_insert_per_statement' => true,
34 | 'ternary_operator_spaces' => true,
35 | 'unary_operator_spaces' => true,
36 | 'visibility_required' => [
37 | 'elements' => [
38 | 'const',
39 | 'method',
40 | 'property',
41 | ],
42 | ],
43 | ])
44 | ->setFinder(
45 | PhpCsFixer\Finder::create()
46 | ->exclude('vendor')
47 | ->in([__DIR__.'/src/', __DIR__.'/tests/'])
48 | )
49 | ;
50 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "overtrue/laravel-stateless-session",
3 | "description": "A lightweight middleware to make api routing session capable.",
4 | "license": "MIT",
5 | "authors": [
6 | {
7 | "name": "overtrue",
8 | "email": "anzhengchao@gmail.com"
9 | }
10 | ],
11 | "require": {
12 | "laravel/framework": "^9.0"
13 | },
14 | "autoload": {
15 | "psr-4": {
16 | "Overtrue\\LaravelStatelessSession\\": "src"
17 | }
18 | },
19 | "autoload-dev": {
20 | "psr-4": {
21 | "Tests\\": "tests"
22 | }
23 | },
24 | "require-dev": {
25 | "mockery/mockery": "^1.4.4",
26 | "phpunit/phpunit": "^9.5.8",
27 | "orchestra/testbench": "^7.0",
28 | "friendsofphp/php-cs-fixer": "^3.0",
29 | "jetbrains/phpstorm-attributes": "^1.0"
30 | },
31 | "extra": {
32 | "hooks": {
33 | "pre-commit": [
34 | "composer check-style",
35 | "composer test"
36 | ],
37 | "pre-push": [
38 | "composer test"
39 | ]
40 | }
41 | },
42 | "scripts": {
43 | "post-update-cmd": [
44 | "cghooks remove",
45 | "cghooks add --ignore-lock",
46 | "cghooks update"
47 | ],
48 | "post-merge": "composer install",
49 | "post-install-cmd": [
50 | "cghooks remove",
51 | "cghooks add --ignore-lock",
52 | "cghooks update"
53 | ],
54 | "cghooks": "vendor/bin/cghooks",
55 | "check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi",
56 | "fix-style": "php-cs-fixer fix --using-cache=no --ansi",
57 | "test": "phpunit --colors"
58 | },
59 | "scripts-descriptions": {
60 | "test": "Run all tests.",
61 | "check-style": "Run style checks (only dry run - no fixing!).",
62 | "fix-style": "Run style checks and fix violations."
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Laravel stateless session
2 | ---
3 |
4 | A lightweight middleware to make api routing session capable.
5 |
6 | [](https://github.com/sponsors/overtrue)
7 |
8 | ## Installing
9 |
10 | ```shell
11 | $ composer require overtrue/laravel-stateless-session -vvv
12 | ```
13 |
14 | ## Usage
15 |
16 | Add the middleware (`\Overtrue\LaravelStatelessSession\Http\Middleware\SetSessionFromHeaders`) to api route group.
17 |
18 | ```php
19 | protected $middlewareGroups = [
20 | //...
21 | 'api' => [
22 | \Overtrue\LaravelStatelessSession\Http\Middleware\SetSessionFromHeaders::class,
23 | \Illuminate\Session\Middleware\StartSession::class,
24 | 'throttle:api',
25 | \Illuminate\Routing\Middleware\SubstituteBindings::class,
26 | ],
27 | ];
28 | ```
29 |
30 | ## Response header
31 |
32 | This middleware will set session id to response headers with name 'x-session', if you want to update the header name, you can add `header` to `config/session.php`:
33 |
34 | *config/session.php*
35 | ```php
36 | 'header' => 'x-session',
37 | ```
38 |
39 | ## Request header
40 |
41 | The api request must set the last session id to headers. Using axios as an example, we need to listen to the api response, retrieve the session ID from response headers and store it in local storage, then retrieve it and add it to the request header:
42 |
43 | ```php
44 | axios.interceptors.request.use(function (config) {
45 | config.headers['x-session'] = 'session id from your localstorage';
46 | return config;
47 | }, function (error) {
48 | return Promise.reject(error);
49 | });
50 |
51 | // Store the response session id
52 | axios.interceptors.response.use(function (response) {
53 | if (response.headers['x-session']) {
54 | // store session id to localstorage
55 | }
56 | return response;
57 | }, function (error) {
58 | return Promise.reject(error);
59 | });
60 | ```
61 |
62 | ## Contributing
63 |
64 | You can contribute in one of three ways:
65 |
66 | 1. File bug reports using the [issue tracker](https://github.com/overtrue/laravel-package/issues).
67 | 2. Answer questions or fix bugs on the [issue tracker](https://github.com/overtrue/laravel-package/issues).
68 | 3. Contribute new features or update the wiki.
69 |
70 | _The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._
71 |
72 | ## Project supported by JetBrains
73 |
74 | Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
75 |
76 | [](https://www.jetbrains.com/?from=https://github.com/overtrue)
77 |
78 |
79 | ## PHP 扩展包开发
80 |
81 | > 想知道如何从零开始构建 PHP 扩展包?
82 | >
83 | > 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
84 |
85 | ## License
86 |
87 | MIT
88 |
--------------------------------------------------------------------------------