Skip to content

Commit accb4ea

Browse files
authored
Improve custom routing mode (#25)
* Use query param for activate token * Update URL tags * Ensure route is still used in auto mode * Correct defaults * Ensure form actions still work in custom mode * Cleanup * Update docs * Cleanup * Cleanup * Cleanup
1 parent 64ee580 commit accb4ea

File tree

6 files changed

+59
-40
lines changed

6 files changed

+59
-40
lines changed

config/statamic/memberbox.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| Enable Account
88
|--------------------------------------------------------------------------
99
|
10-
| Enable the user account pages.
10+
| Enable the automatic user account pages.
1111
|
1212
*/
1313

@@ -18,7 +18,8 @@
1818
| Enable Directory
1919
|--------------------------------------------------------------------------
2020
|
21-
| Enable the user directory pages. You'll also need to uncomment the routes.
21+
| Enable the automatic user directory pages. You'll also need to uncomment
22+
| the routes.
2223
|
2324
| WARNING:
2425
| Enabling the user directory will expose user data publicly. Make sure your

docs/implementation.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ Check the [customisation](customisation.html) documentation for details on custo
1313

1414
---
1515

16-
## Setting up the account and directory pages
16+
## Using the automatic account and directory pages
1717

18-
To enable the account pages set the `statamic.memberbox.enable_account` config option to true (enabled by default):
18+
Memberbox can automatically create account and directory routes/pages for you, and comes with starter templates you can customise to match your site design. To enable the automatic account pages set the `statamic.memberbox.enable_account` config option to true (enabled by default):
1919

2020
```
2121
'enable_account' => true,
2222
```
2323

24-
To enable the directory pages set the `statamic.memberbox.enable_directory` config option to true and uncomment the routes:
24+
To enable the automatic directory pages set the `statamic.memberbox.enable_directory` config option to true and uncomment the routes:
2525

2626
```
2727
'enable_directory' => true,
@@ -33,11 +33,11 @@ To enable the directory pages set the `statamic.memberbox.enable_directory` conf
3333
],
3434
```
3535

36-
> *Warning:* Enabling the user directory will expose user data publicly. Make sure your templates only output the data you want to be public!
36+
:::warning
37+
Enabling the user directory will expose user data publicly. Make sure your templates only output the data you want to be public!
38+
:::
3739

38-
---
39-
40-
## Editing the page templates
40+
### Editing the page templates
4141

4242
The starter templates have been built with the [Starters Creek](https://statamic.com/starter-kits/statamic/starters-creek) kit, which uses Tailwind CSS. To customise these to match your site's design publish the view templates:
4343

@@ -57,6 +57,12 @@ The starter templates use Antlers front matter to set a title variable which you
5757

5858
---
5959

60+
## Using Statamic entries for your account and directory pages
61+
62+
If you would prefer to use pages managed through Statamic as your account and directory pages you should switch the `enable_account` and `enable_directory` options off, and then populate the `routes` array with the URLs of your Statamic entries. Setting these will ensure that the URL tags and activation email links work as expected.
63+
64+
---
65+
6066
## Linking to the pages from your templates
6167

6268
Memberbox provides a set of page URL tags for linking to the pages, check the [tags reference](tags.html#user-form-page-url-tags) for a full list. Here's an example header template that shows how you might implement these in your site:

routes/web.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
Route::statamic($route, 'statamic-memberbox::web.change_password')->name('change_password');
3232
Route::post('/!/statamic-memberbox/change_password', 'UsersController@changePassword')->name('change_password.action');
3333
}
34+
} else {
35+
if (config('statamic.memberbox.routes.profile')) {
36+
Route::post('/!/statamic-memberbox/profile', 'UsersController@profile')->name('profile.action');
37+
}
38+
39+
if (config('statamic.memberbox.routes.change_password')) {
40+
Route::post('/!/statamic-memberbox/change_password', 'UsersController@changePassword')->name('change_password.action');
41+
}
3442
}
3543
if (config('statamic.memberbox.enable_directory', false)) {
3644
if ($route = config('statamic.memberbox.routes.index')) {

src/Http/Controllers/CP/MembersController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use JackSleight\StatamicMemberbox\Notifications\ActivateAccount;
99
use Statamic\Auth\Passwords\PasswordReset;
1010
use Statamic\Contracts\Auth\User as UserContract;
11-
use Statamic\CP\Column;
1211
use Statamic\Exceptions\FatalException;
1312
use Statamic\Exceptions\NotFoundHttpException;
1413
use Statamic\Facades\File;
@@ -143,7 +142,11 @@ public function store(Request $request)
143142

144143
$user->save();
145144

146-
PasswordReset::resetFormroute('statamic-memberbox.activate');
145+
if (config('statamic.memberbox.enable_account', true)) {
146+
PasswordReset::resetFormRoute('statamic-memberbox.activate');
147+
} else {
148+
PasswordReset::resetFormUrl(url(config('statamic.memberbox.routes.activate')));
149+
}
147150
PasswordReset::redirectAfterReset(null);
148151
$token = $user->generateActivateAccountToken();
149152

src/Protectors/Member.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Tags/UserTags.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use JackSleight\StatamicMemberbox\Facades\Member;
66
use Statamic\Facades\User;
7-
use Statamic\Tags\Concerns;
7+
use Statamic\Support\Arr;
88
use Statamic\Auth\UserTags as StatamicUserTags;
99

1010
class UserTags extends StatamicUserTags
@@ -109,7 +109,7 @@ public function changePasswordForm()
109109

110110
public function registerUrl()
111111
{
112-
return route('statamic-memberbox.register');
112+
return $this->accountRouteUrl('register');
113113
}
114114

115115
public function loginUrl()
@@ -122,36 +122,57 @@ public function loginUrl()
122122
: $append;
123123
}
124124

125-
return route('statamic-memberbox.login', ['redirect' => $redirect]);
125+
return $this->accountRouteUrl('login', ['redirect' => $redirect]);
126126
}
127127

128128
public function profileUrl()
129129
{
130-
return route('statamic-memberbox.profile');
130+
return $this->accountRouteUrl('profile');
131131
}
132132

133133
public function forgotPasswordUrl()
134134
{
135-
return route('statamic-memberbox.forgot_password');
135+
return $this->accountRouteUrl('forgot_password');
136136
}
137137

138138
public function resetPasswordUrl()
139139
{
140-
return route('statamic-memberbox.reset_password');
140+
return $this->accountRouteUrl('reset_password');
141141
}
142142

143143
public function changePasswordUrl()
144144
{
145-
return route('statamic-memberbox.change_password');
145+
return $this->accountRouteUrl('change_password');
146146
}
147147

148148
public function indexUrl()
149149
{
150-
return route('statamic-memberbox.index');
150+
return $this->directoryRouteUrl('index');
151151
}
152152

153153
public function showUrl()
154154
{
155-
return route('statamic-memberbox.show', $this->params->all());
155+
return $this->directoryRouteUrl('show', $this->params->all());
156+
}
157+
158+
protected function accountRouteUrl($name, array $params = [])
159+
{
160+
if (config('statamic.memberbox.enable_account', true)) {
161+
return route('statamic-memberbox.'.$name, $params);
162+
}
163+
return $this->routeUrl($name, $params);
164+
}
165+
166+
protected function directoryRouteUrl($name, array $params = [])
167+
{
168+
if (config('statamic.memberbox.enable_directory', false)) {
169+
return route('statamic-memberbox.'.$name, $params);
170+
}
171+
return $this->routeUrl($name, $params);
172+
}
173+
174+
protected function routeUrl($name, array $params = [])
175+
{
176+
return url(config('statamic.memberbox.routes.'.$name)).($params ? ('?'.Arr::query($params)) : null);
156177
}
157178
}

0 commit comments

Comments
 (0)