|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Auth; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use App\Controllers\Controller; |
|
7
|
|
|
use Respect\Validation\Validator as v; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class AuthController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
public function getSignOut($request,$response) |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
$this->auth->logout(); |
|
|
|
|
|
|
15
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function getSignIn($request,$response) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
return $this->view->render($response,'auth/signin.twig'); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function postSignIn($request,$response) |
|
24
|
|
|
{ |
|
25
|
|
|
$auth = $this->auth->attempt( |
|
|
|
|
|
|
26
|
|
|
$request->getParam('email'), |
|
27
|
|
|
$request->getParam('password') |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
if (!$auth) { |
|
31
|
|
|
$this->flash->addMessage('error','Could not sign you in with those details'); |
|
|
|
|
|
|
32
|
|
|
return $response->withRedirect($this->router->pathFor('auth.signin')); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getSignUp($request,$response) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
return $this->view->render($response,'auth/signup.twig'); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function postSignUp($request,$response) |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
$validation = $this->validator->validate($request,[ |
|
|
|
|
|
|
47
|
|
|
'email' => v::noWhitespace()->notEmpty()->email()->emailAvailable(), |
|
48
|
|
|
'name' => v::noWhitespace()->notEmpty()->alpha(), |
|
49
|
|
|
'password' => v::noWhitespace()->notEmpty(), |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
if ($validation->failed()) { |
|
53
|
|
|
return $response->withRedirect($this->router->pathFor('auth.signup')); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$user = User::create([ |
|
57
|
|
|
'email' => $request->getParam('email'), |
|
58
|
|
|
'name' => $request->getParam('name'), |
|
59
|
|
|
'password' => password_hash($request->getParam('password'),PASSWORD_DEFAULT), |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
$this->flash->addMessage('info','You have been signed up'); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$this->auth->attempt($user->email,$request->getParam('password')); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
return $response->withRedirect($this->router->pathFor('home')); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.