-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.php
More file actions
executable file
·73 lines (61 loc) · 2.58 KB
/
AuthController.php
File metadata and controls
executable file
·73 lines (61 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
use \Holded\BaseController;
class AuthController extends BaseController
{
/**
* Pages
*/
public function signIn($request, $response, $args)
{
if($this->auth->user){
return $response->withRedirect($this->router->pathFor('dashboard.index'));
}
return $this->renderer->render($response, 'auth/sign_in.phtml', $this->templateArgs($args));
}
public function signUp($request, $response, $args)
{
if($this->auth->user){
return $response->withRedirect($this->router->pathFor('dashboard.index'));
}
return $this->renderer->render($response, 'auth/sign_up.phtml', $this->templateArgs($args));
}
/**
* Submit actions
*/
public function login($request, $response, $args)
{
$userFields = $request->getParsedBody();
$success = $this->auth->login($userFields);
if(!$success){
$this->flash->addMessage('error', 'User does not exist or password does not match');
return $response->withRedirect($this->router->pathFor('auth.signin'));
}
return $response->withRedirect($this->router->pathFor('dashboard.index'));
}
public function register($request, $response, $args)
{
$userFields = $request->getParsedBody();
$username = $userFields['username'];
$password = $userFields['password'];
$name = $userFields['name'];
if(empty($username) || empty($password)){
$this->flash->addMessage('error', 'Username and password must be provided');
return $response->withRedirect($this->router->pathFor('auth.signup'));
}
if(empty($name)){
$this->flash->addMessage('error', 'You must provide a name');
return $response->withRedirect($this->router->pathFor('auth.signup'));
}
if($this->auth->userExists($username)){
$this->flash->addMessage('error', 'User already exists');
return $response->withRedirect($this->router->pathFor('auth.signup'));
}
if($userFields['password-confirmation'] != $userFields['password']){
$this->flash->addMessage('error', 'Password fields do not match');
return $response->withRedirect($this->router->pathFor('auth.signup'));
}
$result = $this->auth->createUser($userFields);
$this->flash->addMessage('success', 'User created! Please, login');
return $response->withRedirect($this->router->pathFor('auth.signin'));
}
}