44
55use Illuminate \Http \Request ;
66use App \Http \Controllers \Controller ;
7+ use App \Exceptions \VerifyEmailException ;
8+ use Illuminate \Contracts \Auth \MustVerifyEmail ;
9+ use Illuminate \Validation \ValidationException ;
710use Illuminate \Foundation \Auth \AuthenticatesUsers ;
811
912class LoginController extends Controller
@@ -30,20 +33,25 @@ protected function attemptLogin(Request $request)
3033 {
3134 $ token = $ this ->guard ()->attempt ($ this ->credentials ($ request ));
3235
33- if ($ token ) {
34- $ this ->guard ()->setToken ($ token );
36+ if (! $ token ) {
37+ return false ;
38+ }
3539
36- return true ;
40+ $ user = $ this ->guard ()->user ();
41+ if ($ user instanceof MustVerifyEmail && ! $ user ->hasVerifiedEmail ()) {
42+ return false ;
3743 }
3844
39- return false ;
45+ $ this ->guard ()->setToken ($ token );
46+
47+ return true ;
4048 }
4149
4250 /**
4351 * Send the response after the user was authenticated.
4452 *
4553 * @param \Illuminate\Http\Request $request
46- * @return \Illuminate\Http\Response
54+ * @return \Illuminate\Http\JsonResponse
4755 */
4856 protected function sendLoginResponse (Request $ request )
4957 {
@@ -52,11 +60,31 @@ protected function sendLoginResponse(Request $request)
5260 $ token = (string ) $ this ->guard ()->getToken ();
5361 $ expiration = $ this ->guard ()->getPayload ()->get ('exp ' );
5462
55- return [
63+ return response ()-> json ( [
5664 'token ' => $ token ,
5765 'token_type ' => 'bearer ' ,
5866 'expires_in ' => $ expiration - time (),
59- ];
67+ ]);
68+ }
69+
70+ /**
71+ * Get the failed login response instance.
72+ *
73+ * @param \Illuminate\Http\Request $request
74+ * @return \Illuminate\Http\JsonResponse
75+ *
76+ * @throws \Illuminate\Validation\ValidationException
77+ */
78+ protected function sendFailedLoginResponse (Request $ request )
79+ {
80+ $ user = $ this ->guard ()->user ();
81+ if ($ user instanceof MustVerifyEmail && ! $ user ->hasVerifiedEmail ()) {
82+ throw VerifyEmailException::forUser ($ user );
83+ }
84+
85+ throw ValidationException::withMessages ([
86+ $ this ->username () => [trans ('auth.failed ' )],
87+ ]);
6088 }
6189
6290 /**
0 commit comments