Completed
Push — master ( 03b333...573f2e )
by Abdelrahman
02:45
created

AuthenticationController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Frontend;
17
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\Lang;
21
use Rinvex\Fort\Guards\SessionGuard;
22
use Rinvex\Fort\Http\Requests\UserAuthentication;
23
use Rinvex\Fort\Http\Controllers\AbstractController;
24
25
class AuthenticationController extends AbstractController
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $middlewareWhitelist = ['logout'];
31
32
    /**
33
     * Create a new authentication controller instance.
34
     *
35
     * @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...
36
     */
37
    public function __construct()
38
    {
39
        $this->middleware($this->getGuestMiddleware(), ['except' => $this->middlewareWhitelist]);
40
    }
41
42
    /**
43
     * Show the login form.
44
     *
45
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
46
     */
47
    public function showLogin()
48
    {
49
        return view('rinvex.fort::authentication.login');
50
    }
51
52
    /**
53
     * Process to the login form.
54
     *
55
     * @param \Rinvex\Fort\Http\Requests\UserAuthentication $request
56
     *
57
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
58
     */
59
    public function processLogin(UserAuthentication $request)
60
    {
61
        // Prepare variables
62
        $remember    = $request->has('remember');
63
        $loginField  = get_login_field($request->get('loginfield'));
64
        $credentials = [
65
            $loginField => $request->input('loginfield'),
66
            'password'  => $request->input('password'),
67
        ];
68
69
        $result = Auth::guard($this->getGuard())->attempt($credentials, $remember);
70
71
        return $this->getLoginResponse($request, $result);
72
    }
73
74
    /**
75
     * Logout currently logged in user.
76
     *
77
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
78
     */
79
    public function logout()
80
    {
81
        $result = Auth::guard($this->getGuard())->logout();
82
83
        return intend([
84
            'intended' => route('home'),
85
            'with'     => ['rinvex.fort.alert.warning' => Lang::get($result)],
86
        ]);
87
    }
88
89
    /**
90
     * Get login response upon the given request & result.
91
     *
92
     * @param \Illuminate\Http\Request $request
93
     * @param string                   $result
94
     *
95
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
96
     */
97
    protected function getLoginResponse(Request $request, $result)
98
    {
99
        switch ($result) {
100
            // Too many failed logins, user locked out
101
            case SessionGuard::AUTH_LOCKED_OUT:
102
                $seconds = Auth::guard($this->getGuard())->secondsRemainingOnLockout($request);
103
104
                return intend([
105
                    'intended'   => route('home'),
106
                    'withInput'  => $request->only('loginfield', 'remember'),
107
                    'withErrors' => ['loginfield' => Lang::get($result, ['seconds' => $seconds])],
108
                ]);
109
110
            // Valid credentials, but user is unverified; Can NOT login!
111
            case SessionGuard::AUTH_UNVERIFIED:
112
                return intend([
113
                    'intended'   => route('rinvex.fort.verification.email'),
114
                    'withErrors' => ['email' => Lang::get($result)],
115
                ]);
116
117
            // Valid credentials, but user is moderated; Can NOT login!
118
            case SessionGuard::AUTH_MODERATED:
119
                return intend([
120
                    'home'       => true,
121
                    'withErrors' => ['rinvex.fort.auth.moderated' => Lang::get($result)],
122
                ]);
123
124
            // Wrong credentials, failed login
125
            case SessionGuard::AUTH_FAILED:
126
                return intend([
127
                    'back'       => true,
128
                    'withInput'  => $request->only('loginfield', 'remember'),
129
                    'withErrors' => ['loginfield' => Lang::get($result)],
130
                ]);
131
132
            // Two-Factor authentication required
133
            case SessionGuard::AUTH_TWOFACTOR_REQUIRED:
134
                $route = ! isset(session('rinvex.fort.twofactor.methods')['totp']) ? 'rinvex.fort.verification.phone' : 'rinvex.fort.verification.phone.verify';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 160 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
135
136
                return intend([
137
                    'route' => $route,
138
                    'with'  => ['rinvex.fort.alert.warning' => Lang::get($result)],
139
                ]);
140
141
            // Login successful and everything is fine!
142
            case SessionGuard::AUTH_LOGIN:
143
            default:
144
                return intend([
145
                    'intended' => route('home'),
146
                    'with'     => ['rinvex.fort.alert.success' => Lang::get($result)],
147
                ]);
148
        }
149
    }
150
}
151