Skip to content

Commit 1497e97

Browse files
committed
readme
1 parent 90744da commit 1497e97

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

README.md

+16-18
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,37 @@ composer require binarcode/laravel-stateless-session
2424

2525
## Usage
2626

27-
To initiate the session you can use this middleware:
27+
1. Trigger session, make a GET request to: `/api/csrf-header`. This will return a header with the session key and an optional header with CSRF token `XSRF-TOKEN`.
28+
The header name could be configured in: `stateless.header`
2829

29-
``` php
30-
->middleware(\Binarcode\LaravelStatelessSession\Http\Middleware\StartStatelessSession::class)
31-
```
32-
33-
To protect some routes with CSRF token just use this middleware:
30+
2. Use this session key for every request you want to take care of the session.
3431

35-
``` php
36-
->middleware([
37-
\Binarcode\LaravelStatelessSession\Http\Middleware\StartStatelessSession::class,
38-
\Binarcode\LaravelStatelessSession\Http\Middleware\VerifyHeaderCsrfToken::class,
39-
])
40-
// this will return back a response header `XSRF-TOKEN`
32+
3. If you want to benefit of the CSRF protection of your requests, you should add the follow middleware to your routes:
33+
```php
34+
->middleware(Binarcode\LaravelStatelessSession\Http\Middleware\VerifyHeaderCsrfToken::class);
4135
```
4236

37+
Now the server will return 419 (Page expired code). Unless you send back a request header named: `X-CSRF-TOKEN` with the value received by the first GET request in the `XSRF-TOKEN` header.
4338

44-
Any GET request with `stateless.session` or `stateless.csrf` will return back a response header with key
45-
configured in `config('stateless.header')`.
39+
That's it.
4640

47-
This header should be sent back to the server with the same name, so the SessionManager could find the right session.
41+
At this point you have CSRF protection.
4842

49-
If the request should perform a csrf check, just add a `X-CSRF-TOKEN` with the value received in the previous request
50-
under `XSRF-TOKEN` header name.
43+
And you can play with `SessionManager` and use the `session()` helper to store/get information (e.g. flash sessions).
5144

5245
## Config
5346

54-
The API will inject into headers the session key. The session key name could be configured in the:
47+
The lifetime and other options could be set as before in the `session` file.
48+
49+
The `VerifyHeaderCsrfToken` and `StartStatelessSession` middlewares will inject into headers the session key.
50+
51+
The session key name could be configured in the:
5552

5653
```php
5754
stateless.header => env('STATELESS_HEADER', 'X-STATELESS-HEADER')
5855
```
5956

57+
Danger: The key name separators should use `-` not `_` [according with this.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).
6058
### Testing
6159

6260
``` bash

0 commit comments

Comments
 (0)