Skip to content

Commit 08bbc37

Browse files
authored
Merge pull request #603 from kenjis/fix-docs-duplicated
docs: improve quickstart.md and customization.md
2 parents 608cfbe + 4e5e4c6 commit 08bbc37

File tree

2 files changed

+91
-90
lines changed

2 files changed

+91
-90
lines changed

docs/customization.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- [Customizing Shield](#customizing-shield)
44
- [Route Configuration](#route-configuration)
55
- [Custom Redirect URLs](#custom-redirect-urls)
6+
- [Customize Login Redirect](#customize-login-redirect)
7+
- [Customize Register Redirect](#customize-register-redirect)
8+
- [Customize Logout Redirect](#customize-logout-redirect)
69
- [Extending the Controllers](#extending-the-controllers)
710
- [Integrating Custom View Libraries](#integrating-custom-view-libraries)
811
- [Custom Validation Rules](#custom-validation-rules)
@@ -26,20 +29,24 @@ $routes->get('register', '\App\Controllers\Auth\RegisterController::registerView
2629

2730
## Custom Redirect URLs
2831

29-
By default, a successful login or register attempt will all redirect to `/`, while a logout action
30-
will redirect to a [named route](https://codeigniter.com/user_guide/incoming/routing.html#using-named-routes "See routing docs") `login` or a *URI path* `/login`. You can change the default URLs used within the `Auth` config file:
32+
### Customize Login Redirect
33+
34+
You can customize where a user is redirected to on login with the `loginRedirect()` method of the **app/Config/Auth.php** config file. This is handy if you want to redirect based on user group or other criteria.
3135

3236
```php
33-
public array $redirects = [
34-
'register' => '/',
35-
'login' => '/',
36-
'logout' => 'login',
37-
];
37+
public function loginRedirect(): string
38+
{
39+
$url = auth()->user()->inGroup('admin')
40+
? '/admin'
41+
: setting('Auth.redirects')['login'];
42+
43+
return $this->getUrl($url);
44+
}
3845
```
3946

4047
Oftentimes, you will want to have different redirects for different user groups. A simple example
4148
might be that you want admins redirected to `/admin` while all other groups redirect to `/`.
42-
The `Auth` config file also includes methods that you can add additional logic to in order to
49+
The **app/Config/Auth.php** config file also includes methods that you can add additional logic to in order to
4350
achieve this:
4451

4552
```php
@@ -55,6 +62,32 @@ public function loginRedirect(): string
5562
}
5663
```
5764

65+
### Customize Register Redirect
66+
67+
You can customize where a user is redirected to after registration in the `registerRedirect()` method of the **app/Config/Auth.php** config file.
68+
69+
```php
70+
public function registerRedirect(): string
71+
{
72+
$url = setting('Auth.redirects')['register'];
73+
74+
return $this->getUrl($url);
75+
}
76+
```
77+
78+
### Customize Logout Redirect
79+
80+
The logout redirect can also be overridden by the `logoutRedirect()` method of the **app/Config/Auth.php** config file. This will not be used as often as login and register, but you might find the need. For example, if you programatically logged a user out you might want to take them to a page that specifies why they were logged out. Otherwise, you might take them to the home page or even the login page.
81+
82+
```php
83+
public function logoutRedirect(): string
84+
{
85+
$url = setting('Auth.redirects')['logout'];
86+
87+
return $this->getUrl($url);
88+
}
89+
```
90+
5891
## Extending the Controllers
5992

6093
Shield has the following controllers that can be extended to handle

docs/quickstart.md

Lines changed: 50 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,45 @@
22

33
Learning any new authentication system can be difficult, especially as they get more flexible and sophisticated. This guide is intended to provide short examples for common actions you'll take when working with Shield. It is not intended to be the exhaustive documentation for each section. That's better handled through the area-specific doc files.
44

5-
NOTE: The examples assume that you have run the setup script and that you have copies of the `Auth` and `AuthGroups` config files in your application's `app/Config` folder.
5+
> **Note** The examples assume that you have run the setup script and that you have copies of the `Auth` and `AuthGroups` config files in your application's **app/Config** folder.
66
77
- [Quick Start Guide](#quick-start-guide)
88
- [Authentication Flow](#authentication-flow)
9-
- [Customize register redirect](#customize-register-redirect)
10-
- [Customize login redirect](#customize-login-redirect)
11-
- [Customize logout redirect](#customize-logout-redirect)
12-
- [Customize Remember-me functionality](#customize-remember-me-functionality)
13-
- [Change Access Token Lifetime](#change-access-token-lifetime)
14-
- [Enable Account Activation via Email](#enable-account-activation-via-email)
15-
- [Enable Two-Factor Authentication](#enable-two-factor-authentication)
9+
- [Configure Config\\Auth](#configure-configauth)
10+
- [Configure Redirect URLs](#configure-redirect-urls)
11+
- [Configure Remember-me Functionality](#configure-remember-me-functionality)
12+
- [Change Access Token Lifetime](#change-access-token-lifetime)
13+
- [Enable Account Activation via Email](#enable-account-activation-via-email)
14+
- [Enable Two-Factor Authentication](#enable-two-factor-authentication)
1615
- [Responding to Magic Link Logins](#responding-to-magic-link-logins)
1716
- [Session Notification](#session-notification)
1817
- [Event](#event)
1918
- [Authorization Flow](#authorization-flow)
20-
- [Change Available Groups](#change-available-groups)
21-
- [Set the Default Group](#set-the-default-group)
22-
- [Change Available Permissions](#change-available-permissions)
23-
- [Assign Permissions to a Group](#assign-permissions-to-a-group)
24-
- [Assign Permissions to a User](#assign-permissions-to-a-user)
19+
- [Configure Config\\AuthGroups](#configure-configauthgroups)
20+
- [Change Available Groups](#change-available-groups)
21+
- [Set the Default Group](#set-the-default-group)
22+
- [Change Available Permissions](#change-available-permissions)
23+
- [Assign Permissions to a Group](#assign-permissions-to-a-group)
24+
- [Assign Permissions to a User](#assign-permissions-to-a-user)
2525
- [Check If a User Has Permission](#check-if-a-user-has-permission)
26-
- [Adding A Group To A User](#adding-a-group-to-a-user)
27-
- [Removing A Group From A User](#removing-a-group-from-a-user)
28-
- [Checking If User Belongs To A Group](#checking-if-user-belongs-to-a-group)
26+
- [Adding a Group To a User](#adding-a-group-to-a-user)
27+
- [Removing a Group From a User](#removing-a-group-from-a-user)
28+
- [Checking If User Belongs To a Group](#checking-if-user-belongs-to-a-group)
2929
- [Managing Users](#managing-users)
3030
- [Creating Users](#creating-users)
3131
- [Deleting Users](#deleting-users)
32-
- [Editing A User](#editing-a-user)
32+
- [Editing a User](#editing-a-user)
3333

3434
## Authentication Flow
3535

36-
If you need everyone to redirect to a single URL after login/logout/register actions, you can modify the `Config\Auth::redirects` array to specify the url to redirect to.
36+
### Configure Config\Auth
37+
38+
#### Configure Redirect URLs
39+
40+
If you need everyone to redirect to a single URL after login/logout/register actions, you can modify the `Config\Auth::$redirects` array in **app/Config/Auth.php**`** to specify the url to redirect to.
41+
42+
By default, a successful login or register attempt will all redirect to `/`, while a logout action
43+
will redirect to a [named route](https://codeigniter.com/user_guide/incoming/routing.html#using-named-routes "See routing docs") `login` or a *URI path* `/login`. You can change the default URLs used within the **`**app/Config/Auth.php** config file:
3744

3845
```php
3946
public array $redirects = [
@@ -43,50 +50,9 @@ public array $redirects = [
4350
];
4451
```
4552

46-
NOTE: This redirect happens after the specified action is complete. In the case of register or login, it might not happen immediately. For example, if you have any Auth Actions specified, they will be redirected when those actions are completed successfully. If no Auth Actions are specified, they will be redirected immediately after registration or login.
47-
48-
### Customize register redirect
49-
50-
You can customize where a user is redirected to after registration in the `registerRedirect` method of the `Auth` config file.
51-
52-
```php
53-
public function registerRedirect(): string
54-
{
55-
$url = setting('Auth.redirects')['register'];
53+
> **Note** This redirect happens after the specified action is complete. In the case of register or login, it might not happen immediately. For example, if you have any Auth Actions specified, they will be redirected when those actions are completed successfully. If no Auth Actions are specified, they will be redirected immediately after registration or login.
5654
57-
return $this->getUrl($url);
58-
}
59-
```
60-
61-
### Customize login redirect
62-
63-
You can further customize where a user is redirected to on login with the `loginRedirect` method of the `Auth` config file. This is handy if you want to redirect based on user group or other criteria.
64-
65-
```php
66-
public function loginRedirect(): string
67-
{
68-
$url = auth()->user()->inGroup('admin')
69-
? '/admin'
70-
: setting('Auth.redirects')['login'];
71-
72-
return $this->getUrl($url);
73-
}
74-
```
75-
76-
### Customize logout redirect
77-
78-
The logout redirect can also be overridden by the `logoutRedirect` method of the `Auth` config file. This will not be used as often as login and register, but you might find the need. For example, if you programatically logged a user out you might want to take them to a page that specifies why they were logged out. Otherwise, you might take them to the home page or even the login page.
79-
80-
```php
81-
public function logoutRedirect(): string
82-
{
83-
$url = setting('Auth.redirects')['logout'];
84-
85-
return $this->getUrl($url);
86-
}
87-
```
88-
89-
### Customize Remember-me functionality
55+
#### Configure Remember-me Functionality
9056

9157
Remember-me functionality is enabled by default for the `Session` handler. While this is handled in a secure manner, some sites may want it disabled. You might also want to change how long it remembers a user and doesn't require additional login.
9258

@@ -99,17 +65,17 @@ public array $sessionConfig = [
9965
];
10066
```
10167

102-
### Change Access Token Lifetime
68+
#### Change Access Token Lifetime
10369

10470
By default, Access Tokens can be used for 1 year since the last use. This can be easily modified in the `Auth` config file.
10571

10672
```php
10773
public int $unusedTokenLifetime = YEAR;
10874
```
10975

110-
### Enable Account Activation via Email
76+
#### Enable Account Activation via Email
11177

112-
> **Note** You need to configure `app/Config/Email.php` to allow Shield to send emails. See [Installation](install.md#initial-setup).
78+
> **Note** You need to configure **app/Config/Email.php** to allow Shield to send emails. See [Installation](install.md#initial-setup).
11379
11480
By default, once a user registers they have an active account that can be used. You can enable Shield's built-in, email-based activation flow within the `Auth` config file.
11581

@@ -120,9 +86,9 @@ public array $actions = [
12086
];
12187
```
12288

123-
### Enable Two-Factor Authentication
89+
#### Enable Two-Factor Authentication
12490

125-
> **Note** You need to configure `app/Config/Email.php` to allow Shield to send emails. See [Installation](install.md#initial-setup).
91+
> **Note** You need to configure **app/Config/Email.php** to allow Shield to send emails. See [Installation](install.md#initial-setup).
12692
12793
Turned off by default, Shield's Email-based 2FA can be enabled by specifying the class to use in the `Auth` config file.
12894

@@ -135,7 +101,7 @@ public array $actions = [
135101

136102
### Responding to Magic Link Logins
137103

138-
> **Note** You need to configure `app/Config/Email.php` to allow Shield to send emails. See [Installation](install.md#initial-setup).
104+
> **Note** You need to configure **app/Config/Email.php** to allow Shield to send emails. See [Installation](install.md#initial-setup).
139105
140106
Magic Link logins allow a user that has forgotten their password to have an email sent with a unique, one-time login link. Once they've logged in you can decide how to respond. In some cases, you might want to redirect them to a special page where they must choose a new password. In other cases, you might simply want to display a one-time message prompting them to go to their account page and choose a new password.
141107

@@ -168,9 +134,11 @@ Events::on('magicLogin', static function () {
168134

169135
## Authorization Flow
170136

171-
### Change Available Groups
137+
### Configure Config\AuthGroups
138+
139+
#### Change Available Groups
172140

173-
The available groups are defined in the `AuthGroups` config file, under the `$groups` property. Add new entries to the array, or remove existing ones to make them available throughout your application.
141+
The available groups are defined in the **app/Config/AuthGroups.php** config file, under the `$groups` property. Add new entries to the array, or remove existing ones to make them available throughout your application.
174142

175143
```php
176144
public array $groups = [
@@ -182,11 +150,11 @@ public array $groups = [
182150
];
183151
```
184152

185-
### Set the Default Group
153+
#### Set the Default Group
186154

187155
When a user registers on your site, they are assigned the group specified at `Config\AuthGroups::$defaultGroup`. Change this to one of the keys in the `$groups` array to update this.
188156

189-
### Change Available Permissions
157+
#### Change Available Permissions
190158

191159
The permissions on the site are stored in the `AuthGroups` config file also. Each one is defined by a string that represents a context and a permission, joined with a decimal point.
192160

@@ -202,7 +170,7 @@ public array $permissions = [
202170
];
203171
```
204172

205-
### Assign Permissions to a Group
173+
#### Assign Permissions to a Group
206174

207175
Each group can have its own specific set of permissions. These are defined in `Config\AuthGroups::$matrix`. You can specify each permission by it's full name, or using the context and an asterisk (*) to specify all permissions within that context.
208176

@@ -217,7 +185,7 @@ public array $matrix = [
217185
];
218186
```
219187

220-
### Assign Permissions to a User
188+
#### Assign Permissions to a User
221189

222190
Permissions can also be assigned directly to a user, regardless of what groups they belong to. This is done programatically on the `User` Entity.
223191

@@ -245,11 +213,11 @@ if (! auth()->user()->can('users.create')) {
245213
}
246214
```
247215

248-
Note: The example above can also be done through a [controller filter](https://codeigniter.com/user_guide/incoming/filters.html) if you want to apply it to multiple pages of your site.
216+
> **Note** The example above can also be done through a [controller filter](https://codeigniter.com/user_guide/incoming/filters.html) if you want to apply it to multiple pages of your site.
249217
250-
### Adding A Group To A User
218+
### Adding a Group To a User
251219

252-
Groups are assigned to a user via the `addGroup` method. You can pass multiple groups in and they will all be assigned to the user.
220+
Groups are assigned to a user via the `addGroup()` method. You can pass multiple groups in and they will all be assigned to the user.
253221

254222
```php
255223
$user = auth()->user();
@@ -263,18 +231,18 @@ $user = auth()->user();
263231
$user->syncGroups('admin', 'beta');
264232
```
265233

266-
### Removing A Group From A User
234+
### Removing a Group From a User
267235

268-
Groups are removed from a user via the `removeGroup` method. Multiple groups may be removed at once by passing all of their names into the method.
236+
Groups are removed from a user via the `removeGroup()` method. Multiple groups may be removed at once by passing all of their names into the method.
269237

270238
```php
271239
$user = auth()->user();
272240
$user->removeGroup('admin', 'beta');
273241
```
274242

275-
### Checking If User Belongs To A Group
243+
### Checking If User Belongs To a Group
276244

277-
You can check if a user belongs to a group with the `inGroup` method.
245+
You can check if a user belongs to a group with the `inGroup()` method.
278246

279247
```php
280248
$user = auth()->user();
@@ -327,9 +295,9 @@ $users = model('UserModel');
327295
$users->delete($user->id, true);
328296
```
329297

330-
NOTE: The User rows use [soft deletes](https://codeigniter.com/user_guide/models/model.html#usesoftdeletes) so they are not actually deleted from the database unless the second parameter is `true`, like above.
298+
> **Note** The User rows use [soft deletes](https://codeigniter.com/user_guide/models/model.html#usesoftdeletes) so they are not actually deleted from the database unless the second parameter is `true`, like above.
331299
332-
### Editing A User
300+
### Editing a User
333301

334302
The `UserModel::save()`, `update()` and `insert()` methods have been modified to ensure that an email or password previously set on the `User` entity will be automatically updated in the correct `UserIdentity` record.
335303

0 commit comments

Comments
 (0)