Skip to content

Commit 6be3162

Browse files
authored
Merge pull request #841 from kenjis/docs-reorganize-2
docs: reorganize (part 2)
2 parents 98d1069 + d8c89bc commit 6be3162

26 files changed

+498
-356
lines changed

docs/customization/integrating_custom_view_libs.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Integrating Custom View Libraries
22

3-
If your application uses a different method to convert view files to HTML than CodeIgniter's built-in `view()` helper you can easily integrate your system anywhere that a view is rendered within Shield. All controllers and actions use the `CodeIgniter\Shield\Traits\Viewable` trait which provides a simple `view()` method that takes the same arguments as the `view()` helper. This allows you to extend the Action or Controller and only change the single method of rendering the view, leaving all of the logic untouched so your app will not need to maintain Shield logic when it doesn't need to change it.
3+
If your application uses a different method to convert view files to HTML than CodeIgniter's built-in `view()` helper, you can easily integrate your system anywhere that a view is rendered within Shield.
4+
5+
All controllers and actions use the `CodeIgniter\Shield\Traits\Viewable` trait which provides a simple `view()` method that takes the same arguments as the `view()` helper. This allows you to extend the Action or Controller and only change the single method of rendering the view, leaving all of the logic untouched so your app will not need to maintain Shield logic when it doesn't need to change it.
46

57
```php
68
use Acme\Themes\Traits\Themeable;

docs/customization/route_config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Customizing Route Configuration
1+
# Customizing Routes
22

33
If you need to customize how any of the auth features are handled, you will likely need to update the routes to point to the correct controllers. You can still use the `service('auth')->routes()` helper, but you will need to pass the `except` option with a list of routes to customize:
44

docs/customization/table_names.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ public array $tables = [
1717

1818
Set the table names that you want in the array values.
1919

20-
> **Note** You must change the table names before running database migrations.
20+
> **Note**
21+
> You must change the table names before running database migrations.

docs/customization/user_provider.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Customizing User Provider
22

33
If you want to customize user attributes, you need to create your own
4-
[User Provider](../concepts.md#user-providers) class.
4+
[User Provider](../getting_started/concepts.md#user-providers) class.
55
The only requirement is that your new class MUST extend the provided `CodeIgniter\Shield\Models\UserModel`.
66

77
Shield has a CLI command to quickly create a custom `UserModel` class by running the following

docs/customization/validation_rules.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Shield has the following rules for registration:
3636
];
3737
```
3838

39-
> **Note** If you customize the table names, the table names
39+
> **Note**
40+
> If you customize the table names, the table names
4041
> (`users` and `auth_identities`) in the above rules will be automatically
4142
> changed. The rules are implemented in
4243
> `RegisterController::getValidationRules()`.
@@ -78,7 +79,8 @@ If you need a different set of rules for registration, you can specify them in y
7879
];
7980
```
8081

81-
> **Note** If you customize the table names, set the correct table names in the
82+
> **Note**
83+
> If you customize the table names, set the correct table names in the
8284
> rules.
8385
8486
## Login
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Authenticators
2+
3+
## Authenticator List
4+
5+
Shield provides the following Authenticators:
6+
7+
- **Session** authenticator provides traditional ID/Password authentication.
8+
It uses username/email/password to authenticate against and stores the user
9+
information in the session. See [Using Session Authenticator](../quick_start_guide/using_session_auth.md) for usage.
10+
- **AccessTokens** authenticator provides stateless authentication using Personal
11+
Access Tokens passed in the HTTP headers.
12+
See [Protecting an API with Access Tokens](../guides/api_tokens.md) for usage.
13+
- **HmacSha256** authenticator provides stateless authentication using HMAC Keys.
14+
See [Protecting an API with HMAC Keys](../guides/api_hmac_keys.md) for usage.
15+
- **JWT** authenticator provides stateless authentication using JSON Web Token. To use this,
16+
you need additional setup. See [JWT Authentication](../addons/jwt.md).

docs/concepts.md renamed to docs/getting_started/concepts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ on the standard Config class if nothing is found in the database.
2121
Shield has a model to handle user persistence. Shield calls this the "User Provider" class.
2222
A default model is provided for you by the `CodeIgniter\Shield\Models\UserModel` class.
2323

24-
You can use your own model to customize user attributes. See [Customizing User Provider](./customization/user_provider.md) for details.
24+
You can use your own model to customize user attributes. See [Customizing User Provider](../customization/user_provider.md) for details.
2525

2626
## User Identities
2727

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Configuration
2+
3+
## Config files
4+
5+
Shield has a lot of Config items. Change the default values as needed.
6+
7+
If you have completed the setup according to this documentation, you will have
8+
the following configuration files:
9+
10+
- **app/Config/Auth.php**
11+
- **app/Config/AuthGroups.php** - For Authorization
12+
- **app/Config/AuthToken.php** - For AccessTokens and HmacSha256 Authentication
13+
- **app/Config/AuthJWT.php** - For JWT Authentication
14+
15+
Note that you do not need to have configuration files for features you do not use.
16+
17+
This section describes the major Config items that are not described elsewhere.
18+
19+
## AccessTokens Authenticator
20+
21+
### Access Token Lifetime
22+
23+
By default, Access Tokens can be used for 1 year since the last use. This can be easily modified in the **app/Config/Auth.php** config file.
24+
25+
```php
26+
public int $unusedTokenLifetime = YEAR;
27+
```

docs/getting_started/install.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Installation
2+
3+
These instructions assume that you have already [installed the CodeIgniter 4 app starter](https://codeigniter.com/user_guide/installation/installing_composer.html) as the basis for your new project, set up your **.env** file, and created a database that you can access via the Spark CLI script.
4+
5+
## Requirements
6+
7+
- [Composer](https://getcomposer.org)
8+
- Codeigniter **v4.2.7** or later
9+
- A created database that you can access via the Spark CLI script
10+
- InnoDB (not MyISAM) is required if MySQL is used.
11+
12+
## Composer Installation
13+
14+
Installation is done through [Composer](https://getcomposer.org). The example assumes you have it installed globally.
15+
If you have it installed as a phar, or otherwise you will need to adjust the way you call composer itself.
16+
17+
```console
18+
composer require codeigniter4/shield
19+
```
20+
21+
### Troubleshooting
22+
23+
#### IMPORTANT: composer error
24+
25+
If you get the following error:
26+
27+
```console
28+
Could not find a version of package codeigniter4/shield matching your minimum-stability (stable).
29+
Require it with an explicit version constraint allowing its desired stability.
30+
```
31+
32+
1. Run the following commands to change your [minimum-stability](https://getcomposer.org/doc/articles/versions.md#minimum-stability) in your project `composer.json`:
33+
34+
```console
35+
composer config minimum-stability dev
36+
composer config prefer-stable true
37+
```
38+
39+
2. Or specify an explicit version:
40+
41+
```console
42+
composer require codeigniter4/shield:dev-develop
43+
```
44+
45+
The above specifies `develop` branch.
46+
See <https://getcomposer.org/doc/articles/versions.md#branches>
47+
48+
```console
49+
composer require codeigniter4/shield:^1.0.0-beta
50+
```
51+
52+
The above specifies `v1.0.0-beta` or later and before `v2.0.0`.
53+
See <https://getcomposer.org/doc/articles/versions.md#caret-version-range->
54+
55+
## Initial Setup
56+
57+
### Command Setup
58+
59+
1. Run the following command. This command handles steps 1-5 of *Manual Setup* and runs the migrations.
60+
61+
```console
62+
php spark shield:setup
63+
```
64+
65+
> **Note**
66+
> If you want to customize table names, you must change the table names
67+
> before running database migrations.
68+
> See [Customizing Table Names](../customization/table_names.md).
69+
70+
2. Configure **app/Config/Email.php** to allow Shield to send emails with the [Email Class](https://codeigniter.com/user_guide/libraries/email.html).
71+
72+
```php
73+
<?php
74+
75+
namespace Config;
76+
77+
use CodeIgniter\Config\BaseConfig;
78+
79+
class Email extends BaseConfig
80+
{
81+
/**
82+
* @var string
83+
*/
84+
public $fromEmail = 'your_mail@example.com';
85+
86+
/**
87+
* @var string
88+
*/
89+
public $fromName = 'your name';
90+
91+
// ...
92+
}
93+
```
94+
95+
### Manual Setup
96+
97+
There are a few setup items to do before you can start using Shield in
98+
your project.
99+
100+
1. Copy the **Auth.php**, **AuthGroups.php**, and **AuthToken.php** from **vendor/codeigniter4/shield/src/Config/** into your project's config folder and update the namespace to `Config`. You will also need to have these classes extend the original classes. See the example below. These files contain all the settings, group, and permission information for your application and will need to be modified to meet the needs of your site.
101+
102+
```php
103+
// new file - app/Config/Auth.php
104+
<?php
105+
106+
declare(strict_types=1);
107+
108+
namespace Config;
109+
110+
// ...
111+
use CodeIgniter\Shield\Config\Auth as ShieldAuth;
112+
113+
class Auth extends ShieldAuth
114+
{
115+
// ...
116+
}
117+
```
118+
119+
2. **Helper Setup** The `setting` helper needs to be included in almost every page. The simplest way to do this is to add it to the `BaseController::initController()` method:
120+
121+
```php
122+
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
123+
{
124+
$this->helpers = array_merge($this->helpers, ['setting']);
125+
126+
// Do Not Edit This Line
127+
parent::initController($request, $response, $logger);
128+
}
129+
```
130+
131+
This requires that all of your controllers extend the `BaseController`, but that's a good practice anyway.
132+
133+
3. **Routes Setup** The default auth routes can be setup with a single call in **app/Config/Routes.php**:
134+
135+
```php
136+
service('auth')->routes($routes);
137+
```
138+
139+
4. **Security Setup** Set `Config\Security::$csrfProtection` to `'session'` (or set `security.csrfProtection = session` in your **.env** file) for security reasons, if you use Session Authenticator.
140+
141+
5. **Migration** Run the migrations.
142+
143+
> **Note**
144+
> If you want to customize table names, you must change the table names
145+
> before running database migrations.
146+
> See [Customizing Table Names](../customization/table_names.md).
147+
148+
```console
149+
php spark migrate --all
150+
```
151+
152+
#### Note: migration error
153+
154+
When you run `spark migrate --all`, if you get `Class "SQLite3" not found` error:
155+
156+
1. Remove sample migration files in **tests/_support/Database/Migrations/**
157+
2. Or install `sqlite3` php extension
158+
159+
6. Configure **app/Config/Email.php** to allow Shield to send emails.
160+
161+
```php
162+
<?php
163+
164+
namespace Config;
165+
166+
use CodeIgniter\Config\BaseConfig;
167+
168+
class Email extends BaseConfig
169+
{
170+
/**
171+
* @var string
172+
*/
173+
public $fromEmail = 'your_mail@example.com';
174+
175+
/**
176+
* @var string
177+
*/
178+
public $fromName = 'your name';
179+
180+
// ...
181+
}
182+
```

docs/guides/api_tokens.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
Access Tokens can be used to authenticate users for your own site, or when allowing third-party developers to access your API. When making requests using access tokens, the token should be included in the `Authorization` header as a `Bearer` token.
44

5-
> **Note** By default, `$authenticatorHeader['tokens']` is set to `Authorization`. You can change this value by setting the `$authenticatorHeader['tokens']` value in the **app/Config/Auth.php** config file.
5+
> **Note**
6+
> By default, `$authenticatorHeader['tokens']` is set to `Authorization`. You can change this value by setting the `$authenticatorHeader['tokens']` value in the **app/Config/Auth.php** config file.
67
78
Tokens are issued with the `generateAccessToken()` method on the user. This returns a `CodeIgniter\Shield\Entities\AccessToken` instance. Tokens are hashed using a SHA-256 algorithm before being saved to the database. The access token returned when you generate it will include a `raw_token` field that contains the plain-text, un-hashed, token. You should display this to your user at once so they have a chance to copy it somewhere safe, as this is the only time this will be available. After this request, there is no way to get the raw token.
89

0 commit comments

Comments
 (0)