|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Controllers\Controller; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
|
|
8
|
|
|
class AuthController extends Controller |
|
9
|
|
|
{ |
|
10
|
|
|
public function getSignOut($request, $response) |
|
11
|
|
|
{ |
|
12
|
|
|
$this->auth->logout(); |
|
13
|
|
|
|
|
14
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function getSignUp($request, $response) |
|
18
|
|
|
{ |
|
19
|
|
|
return $this->view->render($response, 'auth/signup.twig'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getSignIn($request, $response) |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->view->render($response, 'auth/signin.twig'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function postSignUp($request, $response) |
|
28
|
|
|
{ |
|
29
|
|
|
if ($this->validator->validateRegisterForm($request)->getErrorsCount() > 0) { |
|
30
|
|
|
$_SESSION['errors'] = $this->validator->getErrors(); |
|
31
|
|
|
return $response->withRedirect($this->router->pathFor('auth.signup')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$user = User::create([ |
|
35
|
|
|
'email' => $request->getParam('email'), |
|
36
|
|
|
'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT), |
|
37
|
|
|
'name' => $request->getParam('name'), |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
$this->flash->addMessage('success', 'Zostałeś zarejestrowany oraz zalogowany'); |
|
41
|
|
|
|
|
42
|
|
|
$this->auth->attempt($user->email, $request->getParam('password')); |
|
43
|
|
|
|
|
44
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function postSignIn($request, $response) |
|
48
|
|
|
{ |
|
49
|
|
View Code Duplication |
if ($this->validator->validateLoginForm($request)->getErrorsCount() > 0) { |
|
50
|
|
|
$_SESSION['errors'] = $this->validator->getErrors(); |
|
51
|
|
|
return $response->withRedirect($this->router->pathFor('auth.signin')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$auth = $this->auth->attempt( |
|
55
|
|
|
$request->getParam('email'), |
|
56
|
|
|
$request->getParam('password') |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
if (!$auth) { |
|
60
|
|
|
$this->flash->addMessage('danger', 'Podano błędne dane'); |
|
61
|
|
|
return $response->withRedirect($this->router->pathFor('auth.signin')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|