Skip to content

Commit

Permalink
Handle _error_redirect params. Closes #2078.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Jul 15, 2020
1 parent bc925c3 commit cc97074
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 9 deletions.
25 changes: 17 additions & 8 deletions src/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\MessageBag;
use Statamic\Contracts\Auth\User as UserContract;
use Statamic\Facades\Blueprint;
Expand All @@ -15,19 +16,21 @@ class UserController extends Controller

public function login(Request $request)
{
$request->validate([
$validator = Validator::make($request->all(), [
'email' => 'required',
'password' => 'required',
]);

$loggedIn = Auth::attempt(
$request->only('email', 'password'),
$request->has('remember')
);
$loggedIn = $validator->passes()
? Auth::attempt($request->only('email', 'password'), $request->has('remember'))
: false;

$response = redirect($request->input('_redirect', '/'));
$errorResponse = $request->has('_error_redirect') ? redirect($request->input('_error_redirect')) : back();

return $loggedIn
? redirect($request->input('_redirect', '/'))->withSuccess(__('Login successful.'))
: back()->withInput()->withErrors(__('Invalid credentials.'));
? $response->withSuccess(__('Login successful.'))
: $errorResponse->withInput()->withErrors(__('Invalid credentials.'));
}

public function logout()
Expand All @@ -48,7 +51,13 @@ public function register(Request $request)
'password' => 'required|confirmed',
])->rules();

$this->validateWithBag('user.register', $request, $fieldRules);
$validator = Validator::make($request->all(), $fieldRules);

$errorResponse = $request->has('_error_redirect') ? redirect($request->input('_error_redirect')) : back();

if ($validator->fails()) {
return $errorResponse->withErrors($validator->errors(), 'user.register');
}

$values = $fields->process()->values()->except(['email', 'groups', 'roles']);

Expand Down
69 changes: 69 additions & 0 deletions tests/Tags/Form/FormCreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,75 @@ public function it_will_submit_form_and_render_success()
$this->assertEquals(['Submission successful.'], $success[1]);
}

/** @test */
public function it_will_submit_form_and_follow_custom_redirect_with_success()
{
$this->assertEmpty(Form::find('contact')->submissions());

$this
->post('/!/forms/contact', [
'email' => 'san@holo.com',
'message' => 'hello',
'_redirect' => '/submission-successful',
])
->assertSessionHasNoErrors()
->assertLocation('/submission-successful');

$this->assertCount(1, Form::find('contact')->submissions());

$output = $this->tag(<<<'EOT'
{{ form:contact }}
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
<p class="success">{{ success }}</p>
{{ /form:contact }}
EOT
);

preg_match_all('/<p class="error">(.+)<\/p>/U', $output, $errors);
preg_match_all('/<p class="success">(.+)<\/p>/U', $output, $success);

$this->assertEmpty($errors[1]);
$this->assertEquals(['Submission successful.'], $success[1]);
}

/** @test */
public function it_wont_submit_form_and_follow_custom_redirect_with_errors()
{
$this->assertEmpty(Form::find('contact')->submissions());

$this
->post('/!/forms/contact', [
'_error_redirect' => '/submission-error',
])
->assertSessionHasErrors(['email', 'message'], null, 'form.contact')
->assertLocation('/submission-error');

$this->assertCount(0, Form::find('contact')->submissions());

$output = $this->tag(<<<'EOT'
{{ form:contact }}
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
<p class="success">{{ success }}</p>
{{ /form:contact }}
EOT
);

preg_match_all('/<p class="error">(.+)<\/p>/U', $output, $errors);
preg_match_all('/<p class="success">(.+)<\/p>/U', $output, $success);

$expected = [
'The Email Address field is required.',
'The Message field is required.',
];

$this->assertEquals($expected, $errors[1]);
$this->assertEmpty($success[1]);
}

/** @test */
public function it_will_use_redirect_query_param_off_url()
{
Expand Down
40 changes: 39 additions & 1 deletion tests/Tags/User/LoginFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function it_renders_form_with_params()
/** @test */
public function it_renders_form_with_redirects_to_anchor()
{
$output = $this->tag('{{ user:register_form redirect="#form" error_redirect="#form" }}{{ /user:register_form }}');
$output = $this->tag('{{ user:login_form redirect="#form" error_redirect="#form" }}{{ /user:login_form }}');

$this->assertStringContainsString('<input type="hidden" name="_redirect" value="http://localhost#form" />', $output);
$this->assertStringContainsString('<input type="hidden" name="_error_redirect" value="http://localhost#form" />', $output);
Expand Down Expand Up @@ -156,6 +156,44 @@ public function it_will_log_user_in_and_follow_custom_redirect_with_success()
$this->assertEquals(['Login successful.'], $success[1]);
}

/** @test */
public function it_wont_log_user_in_and_follow_custom_error_redirect_with_errors()
{
$this->assertFalse(auth()->check());

User::make()
->email('san@holo.com')
->password('chewy')
->save();

$this
->post('/!/auth/login', [
'token' => 'test-token',
'email' => 'san@holo.com',
'password' => 'wrong',
'_error_redirect' => '/login-error',
])
->assertLocation('/login-error');

$this->assertFalse(auth()->check());

$output = $this->tag(<<<'EOT'
{{ user:login_form }}
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
<p class="success">{{ success }}</p>
{{ /user:login_form }}
EOT
);

preg_match_all('/<p class="error">(.+)<\/p>/U', $output, $errors);
preg_match_all('/<p class="success">(.+)<\/p>/U', $output, $success);

$this->assertEquals(['Invalid credentials.'], $errors[1]);
$this->assertEmpty($success[1]);
}

/** @test */
public function it_will_use_redirect_query_param_off_url()
{
Expand Down
46 changes: 46 additions & 0 deletions tests/Tags/User/RegisterFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,52 @@ public function it_will_register_user_and_follow_custom_redirect_with_success()
$this->assertEquals(['Registration successful.'], $success[1]);
}

/** @test */
public function it_wont_register_user_and_follow_custom_redirect_with_errors()
{
$this->assertNull(User::findByEmail('san@holo.com'));
$this->assertFalse(auth()->check());

$this
->post('/!/auth/register', [
'_error_redirect' => '/registration-error',
])
->assertSessionHasErrors([
'email',
'password',
], null, 'user.register')
->assertLocation('/registration-error');

$this->assertNull(User::findByEmail('san@holo.com'));
$this->assertFalse(auth()->check());

$output = $this->tag(<<<'EOT'
{{ user:register_form }}
<p class="success">{{ success }}</p>
{{ errors }}
<p class="error">{{ value }}</p>
{{ /errors }}
{{ fields }}
<p class="inline-error">{{ error }}</p>
{{ /fields }}
{{ /user:register_form }}
EOT
);

preg_match_all('/<p class="success">(.+)<\/p>/U', $output, $success);
preg_match_all('/<p class="error">(.+)<\/p>/U', $output, $errors);
preg_match_all('/<p class="inline-error">(.+)<\/p>/U', $output, $inlineErrors);

$expected = [
'The email field is required.',
'The password field is required.',
];

$this->assertEmpty($success[1]);
$this->assertEquals($expected, $errors[1]);
$this->assertEquals($expected, $inlineErrors[1]);
}

/** @test */
public function it_will_use_redirect_query_param_off_url()
{
Expand Down

0 comments on commit cc97074

Please sign in to comment.