LoginController::login()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 15
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 1
crap 20
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Session\Flash;
7
use App\User;
8
use Illuminate\Foundation\Auth\AuthenticatesUsers;
9
use Illuminate\Http\Request;
10
11
class LoginController extends Controller
12
{
13
    /*
14
    |--------------------------------------------------------------------------
15
    | Login Controller
16
    |--------------------------------------------------------------------------
17
    |
18
    | This controller handles authenticating users for the application and
19
    | redirecting them to your home screen. The controller uses a trait
20
    | to conveniently provide its functionality to your applications.
21
    |
22
    */
23
24
    use AuthenticatesUsers;
25
26
    /**
27
     * Where to redirect users after login.
28
     *
29
     * @var string
30
     */
31
    protected $redirectTo = '/';
32
33
    /**
34
     * Create a new controller instance.
35
     *
36
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct()
39
    {
40
        $this->middleware('guest', ['except' => 'logout']);
41
    }
42
43
    public function showLoginForm()
44
    {
45
        return view('frontend.auth.login');
46
    }
47
48
    /**
49
     * Handle a login request to the application.
50
     *
51
     * @param  \Illuminate\Http\Request  $request
52
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
53
     */
54
    public function login(Request $request)
55
    {
56
        $this->validateLogin($request);
57
58
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
59
        // the login attempts for this application. We'll key this by the username and
60
        // the IP address of the client making these requests into this application.
61
        if ($this->hasTooManyLoginAttempts($request)) {
62
            $this->fireLockoutEvent($request);
63
64
            return $this->sendLockoutResponse($request);
65
        }
66
67
        // We don't want this user logging in if account is not activated
68
        $userStatus = $this->checkUserEmailVerified($request->get('email'));
69
70
        if ($userStatus == 'unverified') {
71
            return redirect()->back()
72
                ->withInput($request->only($this->username(), 'remember'))
73
                ->with('unverified', 'Account not yet verified or not active');
74
        }
75
76
        // User is verified, authentication can continue
77
        if ($this->attemptLogin($request)) {
78
            Flash::success('Logged in successfully');
79
80
            return $this->sendLoginResponse($request);
81
        }
82
83
        // If the login attempt was unsuccessful we will increment the number of attempts
84
        // to login and redirect the user back to the login form. Of course, when this
85
        // user surpasses their maximum number of attempts they will get locked out.
86
        $this->incrementLoginAttempts($request);
87
88
        return $this->sendFailedLoginResponse($request);
89
    }
90
91
    /**
92
     * Check if User email is verified.
93
     *
94
     * @param $email
95
     * @return string
96
     */
97
    public function checkUserEmailVerified($email)
98
    {
99
        $user = User::whereEmail($email)->first();
100
101
        if ($user and $user->activated == 0) {
102
            return 'unverified';
103
        }
104
    }
105
106
    /**
107
     * Log the user out of the application.
108
     *
109
     * @param  \Illuminate\Http\Request  $request
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function logout(Request $request)
113
    {
114
        $this->guard()->logout();
115
116
        $request->session()->flush();
0 ignored issues
show
Bug introduced by
The method flush() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
        $request->session()->regenerate();
0 ignored issues
show
Bug introduced by
The method regenerate() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
        Flash::success('You have successfully signed out of MyIO!');
121
122
        return redirect('/');
123
    }
124
}
125