Issues (36)

app/Controller/Auth.php (5 issues)

1
<?php
2
/**
3
 * StupidlySimple - A PHP Framework For Lazy Developers.
4
 *
5
 * @author		Fariz Luqman <[email protected]>
6
 * @copyright	2017 Fariz Luqman
7
 * @license		MIT
8
 *
9
 * @link		https://stupidlysimple.github.io/
10
 */
11
12
namespace Controller;
13
14
use Request;
15
use Response;
16
use Sentry;
0 ignored issues
show
The type Sentry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Class Auth.
20
 */
21
class Auth
22
{
23
    private static $successRedirectURL = 'admin';
24
25
    public static function check()
26
    {
27
        if (Sentry::check() !== true) {
28
            Response::redirect('login');
29
        }
30
    }
31
32
    public static function authenticate()
33
    {
34
        try {
35
            // Login credentials
36
            $credentials = [
37
                'email'    => Request::get('email'),
38
                'password' => Request::get('password'),
39
            ];
40
41
            // Authenticate the user
42
            $user = Sentry::authenticate($credentials, false);
0 ignored issues
show
The assignment to $user is dead and can be removed.
Loading history...
Comprehensibility introduced by
Avoid variables with short names like $user. Configured minimum length is 5.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
43
        } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
44
            Response::redirect('login')->with([
45
                'login_message'=> 'Login credentials not supplied',
46
                'type'         => 'alert-danger',
47
            ]);
48
        } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
49
            Response::redirect('login')->with([
50
                'login_message'=> 'Password field is required',
51
                'type'         => 'alert-danger',
52
            ]);
53
        } catch (\Cartalyst\Sentry\Users\WrongPasswordException $e) {
54
            Response::redirect('login')->with([
55
                'login_message'=> 'Wrong password, try again.',
56
                'type'         => 'alert-danger',
57
            ]);
58
        } catch (\Cartalyst\Sentry\Users\UserNotFoundException $e) {
59
            Response::redirect('login')->with([
60
                'login_message'=> 'User not found.',
61
                'type'         => 'alert-danger',
62
            ]);
63
        } catch (\Cartalyst\Sentry\Users\UserNotActivatedException $e) {
64
            Response::redirect('login')->with([
65
                'login_message'=> 'User is not activated.',
66
                'type'         => 'alert-danger',
67
            ]);
68
        } finally {
69
            if (Sentry::check() === true) {
70
                Response::redirect(self::$successRedirectURL)->with([
71
                    'login_message'=> 'Login successful.',
72
                    'type'         => 'alert-success',
73
                ]);
74
            } else {
75
                Response::redirect('login')->with([
76
                    'login_message'=> 'Unable to login',
77
                    'type'         => 'alert-danger',
78
                ]);
79
            }
80
        }
81
    }
82
83
    public static function register()
84
    {
85
        try {
86
            $user = Sentry::register([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $user. Configured minimum length is 5.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
87
                'email'      => Request::get('email'),
88
                'password'   => Request::get('password'),
89
                'first_name' => Request::get('first_name'),
90
                'last_name'  => Request::get('last_name'),
91
            ], $activate = true);
92
        } catch (\Cartalyst\Sentry\Users\LoginRequiredException $e) {
93
            Response::redirect('register')->with([
94
                'login_message'=> 'Login credentials not supplied',
95
                'type'         => 'alert-danger',
96
            ]);
97
        } catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e) {
98
            Response::redirect('register')->with([
99
                'login_message'=> 'Password field is required',
100
                'type'         => 'alert-danger',
101
            ]);
102
        } catch (\Cartalyst\Sentry\Users\UserExistsException $e) {
103
            Response::redirect('register')->with([
104
                'login_message'=> 'User with that login already exist.',
105
                'type'         => 'alert-danger',
106
            ]);
107
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
108
        } finally {
109
            if ($user) {
110
                Response::redirect('login')->with([
111
                    'login_message'=> 'Registration successful. You can now login.',
112
                    'type'         => 'alert-success',
113
                ]);
114
            }
115
        }
116
    }
117
118
    public static function logout()
119
    {
120
        Sentry::logout();
121
        Response::redirect('login')->with([
122
            'login_message'=> 'Logout successful.',
123
            'type'         => 'alert-success',
124
        ]);
125
    }
126
}
127