1+ <?php
2+
3+ namespace Backpack \CRUD \app \Library \Auth ;
4+
5+ use Illuminate \Http \Request ;
6+ use Illuminate \Http \Response ;
7+ use Illuminate \Support \Facades \Auth ;
8+ use Illuminate \Validation \ValidationException ;
9+
10+ trait AuthenticatesUsers
11+ {
12+ use RedirectsUsers, ThrottlesLogins;
13+
14+ /**
15+ * Show the application's login form.
16+ *
17+ * @return \Illuminate\Http\Response
18+ */
19+ public function showLoginForm ()
20+ {
21+ return view ('auth.login ' );
22+ }
23+
24+ /**
25+ * Handle a login request to the application.
26+ *
27+ * @param \Illuminate\Http\Request $request
28+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
29+ *
30+ * @throws \Illuminate\Validation\ValidationException
31+ */
32+ public function login (Request $ request )
33+ {
34+ $ this ->validateLogin ($ request );
35+
36+ // If the class is using the ThrottlesLogins trait, we can automatically throttle
37+ // the login attempts for this application. We'll key this by the username and
38+ // the IP address of the client making these requests into this application.
39+ if (method_exists ($ this , 'hasTooManyLoginAttempts ' ) &&
40+ $ this ->hasTooManyLoginAttempts ($ request )) {
41+ $ this ->fireLockoutEvent ($ request );
42+
43+ return $ this ->sendLockoutResponse ($ request );
44+ }
45+
46+ if ($ this ->attemptLogin ($ request )) {
47+ return $ this ->sendLoginResponse ($ request );
48+ }
49+
50+ // If the login attempt was unsuccessful we will increment the number of attempts
51+ // to login and redirect the user back to the login form. Of course, when this
52+ // user surpasses their maximum number of attempts they will get locked out.
53+ $ this ->incrementLoginAttempts ($ request );
54+
55+ return $ this ->sendFailedLoginResponse ($ request );
56+ }
57+
58+ /**
59+ * Validate the user login request.
60+ *
61+ * @param \Illuminate\Http\Request $request
62+ * @return void
63+ *
64+ * @throws \Illuminate\Validation\ValidationException
65+ */
66+ protected function validateLogin (Request $ request )
67+ {
68+ $ request ->validate ([
69+ $ this ->username () => 'required|string ' ,
70+ 'password ' => 'required|string ' ,
71+ ]);
72+ }
73+
74+ /**
75+ * Attempt to log the user into the application.
76+ *
77+ * @param \Illuminate\Http\Request $request
78+ * @return bool
79+ */
80+ protected function attemptLogin (Request $ request )
81+ {
82+ return $ this ->guard ()->attempt (
83+ $ this ->credentials ($ request ), $ request ->filled ('remember ' )
84+ );
85+ }
86+
87+ /**
88+ * Get the needed authorization credentials from the request.
89+ *
90+ * @param \Illuminate\Http\Request $request
91+ * @return array
92+ */
93+ protected function credentials (Request $ request )
94+ {
95+ return $ request ->only ($ this ->username (), 'password ' );
96+ }
97+
98+ /**
99+ * Send the response after the user was authenticated.
100+ *
101+ * @param \Illuminate\Http\Request $request
102+ * @return \Illuminate\Http\Response
103+ */
104+ protected function sendLoginResponse (Request $ request )
105+ {
106+ $ request ->session ()->regenerate ();
107+
108+ $ this ->clearLoginAttempts ($ request );
109+
110+ if ($ response = $ this ->authenticated ($ request , $ this ->guard ()->user ())) {
111+ return $ response ;
112+ }
113+
114+ return $ request ->wantsJson ()
115+ ? new Response ('' , 204 )
116+ : redirect ()->intended ($ this ->redirectPath ());
117+ }
118+
119+ /**
120+ * The user has been authenticated.
121+ *
122+ * @param \Illuminate\Http\Request $request
123+ * @param mixed $user
124+ * @return mixed
125+ */
126+ protected function authenticated (Request $ request , $ user )
127+ {
128+ //
129+ }
130+
131+ /**
132+ * Get the failed login response instance.
133+ *
134+ * @param \Illuminate\Http\Request $request
135+ * @return \Symfony\Component\HttpFoundation\Response
136+ *
137+ * @throws \Illuminate\Validation\ValidationException
138+ */
139+ protected function sendFailedLoginResponse (Request $ request )
140+ {
141+ throw ValidationException::withMessages ([
142+ $ this ->username () => [trans ('auth.failed ' )],
143+ ]);
144+ }
145+
146+ /**
147+ * Get the login username to be used by the controller.
148+ *
149+ * @return string
150+ */
151+ public function username ()
152+ {
153+ return 'email ' ;
154+ }
155+
156+ /**
157+ * Log the user out of the application.
158+ *
159+ * @param \Illuminate\Http\Request $request
160+ * @return \Illuminate\Http\Response
161+ */
162+ public function logout (Request $ request )
163+ {
164+ $ this ->guard ()->logout ();
165+
166+ $ request ->session ()->invalidate ();
167+
168+ $ request ->session ()->regenerateToken ();
169+
170+ if ($ response = $ this ->loggedOut ($ request )) {
171+ return $ response ;
172+ }
173+
174+ return $ request ->wantsJson ()
175+ ? new Response ('' , 204 )
176+ : redirect ('/ ' );
177+ }
178+
179+ /**
180+ * The user has logged out of the application.
181+ *
182+ * @param \Illuminate\Http\Request $request
183+ * @return mixed
184+ */
185+ protected function loggedOut (Request $ request )
186+ {
187+ //
188+ }
189+
190+ /**
191+ * Get the guard to be used during authentication.
192+ *
193+ * @return \Illuminate\Contracts\Auth\StatefulGuard
194+ */
195+ protected function guard ()
196+ {
197+ return Auth::guard ();
198+ }
199+ }
0 commit comments