|
4 | 4 |
|
5 | 5 | // use Core\App; |
6 | 6 | use Core\Authenticator; |
7 | | -use Core\Session; |
| 7 | +// use Core\Session; |
| 8 | +// use Core\ValidationException; |
8 | 9 | // use Core\Database; |
9 | 10 | // use Core\Validator; |
10 | 11 | use Http\Forms\LoginForm; |
11 | 12 |
|
12 | 13 | // $db = App::resolve(Database::class); |
13 | 14 |
|
14 | 15 | // var_dump('I have been posted!'); |
| 16 | +// $email = $_POST['email']; |
| 17 | +// $password = $_POST['password']; |
| 18 | + |
| 19 | +// tray catch is move to the public/index.php file |
| 20 | +// try { |
| 21 | +$form = LoginForm::validate($attributes = [ |
| 22 | + 'email' => $_POST['email'], |
| 23 | + 'password' => $_POST['password'] |
| 24 | +]); |
| 25 | +// } catch (ValidationException $exception) { |
| 26 | +// Session::flash('errors', $exception->errors); |
| 27 | +// Session::flash('old', $exception->old); |
15 | 28 |
|
16 | | -$email = $_POST['email']; |
17 | | -$password = $_POST['password']; |
| 29 | +// return redirect('/login'); |
| 30 | +// } |
18 | 31 |
|
19 | | -$form = new LoginForm(); |
| 32 | +$signedIn = (new Authenticator()) |
| 33 | + ->attempt( |
| 34 | + $attributes['email'], |
| 35 | + $attributes['password'] |
| 36 | + ); |
| 37 | + |
| 38 | +if (!$signedIn) { |
| 39 | + $form->error( |
| 40 | + 'email', |
| 41 | + 'No matching account found for that email address and password.' |
| 42 | + )->throw(); |
| 43 | +} |
20 | 44 |
|
21 | | -if ($form->validate($email, $password)) { |
22 | | - // $auth = new Authenticator(); |
23 | | - // if you are not passing constructor parameters then you can omit the () |
24 | | - // if ((new Authenticator)->attempt($email, $password)) { |
25 | | - if ((new Authenticator())->attempt($email, $password)) { |
26 | | - redirect('/'); |
27 | | - } |
| 45 | +redirect('/'); |
28 | 46 |
|
29 | | - $form->error('email', 'No matching account found for that email address and password.'); |
30 | | -} |
| 47 | +// and finally lets reverse it i often like to do my guard clauses before the happy path |
| 48 | +// in this case we are doing the happy path first and then the guard clause |
| 49 | +// if ((new Authenticator())->attempt($attributes['email'], $attributes['password'])) { |
| 50 | +// redirect('/'); |
| 51 | +// } |
| 52 | + |
| 53 | +// $form->error('email', 'No matching account found for that email address and password.') |
| 54 | +// ->throw(); |
31 | 55 |
|
32 | 56 | // $_SESSION['errors'] = $form->errors(); |
33 | 57 | // we need to expire above thing after one page load |
34 | 58 |
|
35 | 59 | // $_SESSION['_flash']['errors'] = $form->errors(); |
36 | 60 |
|
37 | | -Session::flash('errors', $form->errors()); |
| 61 | +// Session::flash('errors', $form->errors()); |
38 | 62 |
|
39 | 63 | // old form data old is really common convention |
40 | 64 | // you probably notice this many times there is never a situation where we should manually populate |
41 | 65 | // that password input, you always have to reenter it yourself |
42 | | -Session::flash('old', [ |
43 | | - 'email' => $_POST['email'] |
44 | | -]); |
| 66 | +// Session::flash('old', [ |
| 67 | +// 'email' => $_POST['email'] |
| 68 | +// ]); |
45 | 69 |
|
46 | | -return redirect('/login'); |
| 70 | +// return redirect('/login'); |
47 | 71 |
|
48 | 72 | // return view('session/create.view.php', [ |
49 | 73 | // 'errors' => $form->errors() |
|
0 commit comments