Blame view

api/app/Http/Controllers/Auth/LoginController.php 2.32 KB
0d8c0f816   Thanasis Naskos   initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  <?php
  
  namespace App\Http\Controllers\Auth;
  
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Validation\ValidationException;
  use Illuminate\Foundation\Auth\AuthenticatesUsers;
  
  class LoginController extends Controller
  {
      use AuthenticatesUsers;
  
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this->middleware('guest')->except('logout');
      }
  
      /**
       * Attempt to log the user into the application.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return bool
       */
      protected function attemptLogin(Request $request)
      {
          $token = $this->guard()->attempt($this->credentials($request));
  
          if (! $token) {
              return false;
          }
  
          $user = $this->guard()->user();
          if ($user instanceof MustVerifyEmail && ! $user->hasVerifiedEmail()) {
              return false;
          }
  
          $this->guard()->setToken($token);
  
          return true;
      }
  
      /**
       * Send the response after the user was authenticated.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\JsonResponse
       */
      protected function sendLoginResponse(Request $request)
      {
          $this->clearLoginAttempts($request);
  
          $token = (string) $this->guard()->getToken();
          $expiration = $this->guard()->getPayload()->get('exp');
  
          return response()->json([
              'token' => $token,
              'token_type' => 'bearer',
              'expires_in' => $expiration - time(),
          ]);
      }
  
      /**
       * Get the failed login response instance.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\JsonResponse
       *
       * @throws \Illuminate\Validation\ValidationException
       */
      protected function sendFailedLoginResponse(Request $request)
      {
          $user = $this->guard()->user();
  
          throw ValidationException::withMessages([
              $this->username() => [trans('auth.failed')],
          ]);
      }
  
      /**
       * Log the user out of the application.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function logout(Request $request)
      {
          $this->guard()->logout();
      }
  }