Skip to content

Commit a6f06b8

Browse files
committed
feat: update csrf
1 parent 1f7319b commit a6f06b8

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/modules/anchor/csrf/index.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
</div>
1212

13-
This package is leaf's implementation of CSRF default protection with leaf anchor. It comes separated from leaf anchor because it is not needed in every project you may build.
13+
A CSRF (Cross-Site Request Forgery) attack tricks a user into performing unwanted actions on your website without their knowledge. This can be done by sending a request to your website from another website the user is logged into. To prevent this, Leaf provides a powerful CSRF protection module that handles all the funny business for you.
1414

1515
## Installation
1616

@@ -28,7 +28,13 @@ leaf install csrf
2828

2929
## Basic Usage
3030

31-
After installing leaf CSRF, leaf automatically loads the CSRF package for you, so you don't need to do anything unless you want to configure the CSRF module to match your application requirements.
31+
Once installed, you can enable CSRF protection in your app by passing CSRF config to your app instance. Since CSRF is a Leaf module, it comes with first-class support for Leaf apps.
32+
33+
```php
34+
app()->csrf();
35+
36+
// ... your app
37+
```
3238

3339
### Using CSRF outside of leaf
3440

@@ -46,20 +52,20 @@ Just like every other leaf module, this module also allows you to customize it t
4652

4753
**Available config:**
4854

49-
- **SECRET_KEY** - This is the key with which the token is saved and used in your leaf app. If this is not specified, leaf uses the name `_token` as done in other frameworks like Laravel.
55+
- **secretKey** - This is the key with which the token is saved and used in your leaf app. If this is not specified, leaf uses the name `_token` as done in other frameworks like Laravel.
5056

51-
- **SECRET** - This is the secret key used to encrypt the token. Leaf also has a default secret key set for you. Note that the secret key is attached to a set of unique numbers that not even leaf knows.
57+
- **secret** - This is the secret key used to encrypt the token. Leaf also has a default secret key set for you. Note that the secret key is attached to a set of unique numbers that not even leaf knows.
5258

53-
- **EXCEPT** - This is an array of routes that you want to exclude from the CSRF protection.
59+
- **except** - This is an array of routes that you want to exclude from the CSRF protection.
5460

55-
- **METHODS** - This is an array of HTTP methods to apply CSRF protection to. By default, leaf uses `["POST", "PUT", "PATCH", "DELETE"]`
61+
- **methods** - This is an array of HTTP methods to apply CSRF protection to. By default, leaf uses `["POST", "PUT", "PATCH", "DELETE"]`
5662

5763
```php
5864
use Leaf\Anchor\CSRF;
5965

6066
CSRF::config([
61-
"METHODS" => ["GET"],
62-
"EXCEPT" => ["/"],
67+
'methods' => ['GET'],
68+
'except' => ['/'],
6369
]);
6470
```
6571

@@ -70,13 +76,7 @@ A token is generated under the hood for your application, you can get this token
7076
```php
7177
$csrfToken = Leaf\Anchor\CSRF::token();
7278

73-
>> ["_token" => "TOKEN VALUE"]
74-
```
75-
76-
To make things a bit easier, `token` returns associative array holding the token key name and the token itself. This is an example JSON represenation.
77-
78-
```json
79-
{"_token": "TOKEN VALUE"}
79+
>> "TOKEN VALUE"
8080
```
8181

8282
## Form
@@ -90,27 +90,29 @@ You would usually want to append a hidden input field holding the token to a for
9090
</form>
9191
```
9292

93-
## Functional Mode
94-
95-
Just as with other modules, leaf csrf also ships with global functions that make development a lot easier.
96-
97-
### _token
93+
## Error Handling
9894

99-
This method returns the CSRF token just as done with the `token` method above.
95+
By default, Leaf will output a built-in error page when a CSRF token is invalid. You can customize the messages shown on this page by updating your `config` object.
10096

10197
```php
102-
$csrfToken = _token();
103-
104-
>> ["_token" => "TOKEN VALUE"]
98+
app()->csrf([
99+
'messages.tokenNotFound' => 'Token not found',
100+
'messages.tokenInvalid' => 'Invalid token.',
101+
]);
105102
```
106103

107-
### _csrfField
108-
109-
This directly renders the form field for the CSRF token generated.
104+
This will update the messages shown when a token is not found or invalid. If you want to handle the error yourself, you can pass an error handler to the `csrf` method.
110105

111106
```php
112-
<form ...>
113-
<?php _csrfField(); ?>
114-
...
115-
</form>
107+
app()->csrf([
108+
'onError' => function($error) {
109+
if ($error === "tokenNotFound") {
110+
// handle token not found error
111+
} else {
112+
// handle invalid token error
113+
}
114+
}
115+
]);
116116
```
117+
118+
You can use this to handle the error in any way you want.

0 commit comments

Comments
 (0)