Skip to content

Commit d2aff61

Browse files
authored
Merge pull request #298 from parisiam/fix-register-when-logged
Fix: register when logged in
2 parents 93f607f + 2c4bcfb commit d2aff61

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Controllers/RegisterController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class RegisterController extends BaseController
2727
*/
2828
public function registerView()
2929
{
30+
if (auth()->loggedIn()) {
31+
return redirect()->to(config('Auth')->registerRedirect());
32+
}
33+
3034
// Check if registration is allowed
3135
if (! setting('Auth.allowRegistration')) {
3236
return redirect()->back()->withInput()
@@ -41,6 +45,10 @@ public function registerView()
4145
*/
4246
public function registerAction(): RedirectResponse
4347
{
48+
if (auth()->loggedIn()) {
49+
return redirect()->to(config('Auth')->registerRedirect());
50+
}
51+
4452
// Check if registration is allowed
4553
if (! setting('Auth.allowRegistration')) {
4654
return redirect()->back()->withInput()

tests/Controllers/RegisterTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use CodeIgniter\Test\FeatureTestTrait;
1212
use Config\Services;
1313
use Tests\Support\DatabaseTestCase;
14+
use Tests\Support\FakeUser;
1415

1516
/**
1617
* @internal
1718
*/
1819
final class RegisterTest extends DatabaseTestCase
1920
{
2021
use FeatureTestTrait;
22+
use FakeUser;
2123

2224
protected $namespace;
2325

@@ -146,6 +148,35 @@ public function testRegisterRedirectsToActionIfDefined(): void
146148
]);
147149
}
148150

151+
public function testRegisterRedirectsIfLoggedIn(): void
152+
{
153+
// log them in
154+
session()->set('user', ['id' => $this->user->id]);
155+
156+
$result = $this->withSession()->get('/register');
157+
158+
$result->assertStatus(302);
159+
$result->assertRedirect();
160+
$result->assertRedirectTo(config('Auth')->registerRedirect());
161+
}
162+
163+
public function testRegisterActionRedirectsIfLoggedIn(): void
164+
{
165+
// log them in
166+
session()->set('user', ['id' => $this->user->id]);
167+
168+
$result = $this->withSession()->post('/register', [
169+
'username' => 'JohnDoe',
170+
'email' => 'john.doe@example.com',
171+
'password' => 'secret things might happen here',
172+
'password_confirm' => 'secret things might happen here',
173+
]);
174+
175+
$result->assertStatus(302);
176+
$result->assertRedirect();
177+
$result->assertRedirectTo(config('Auth')->registerRedirect());
178+
}
179+
149180
protected function setupConfig(): void
150181
{
151182
$config = config('Validation');

0 commit comments

Comments
 (0)