-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLoginController.php
146 lines (122 loc) · 4.25 KB
/
LoginController.php
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace App\Http\Controllers\Auth;
use App\Exceptions\LockedException;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Notification;
use TinnyApi\Models\UserModel;
use TinnyApi\Notifications\VerifyEmailNotification;
use TinnyApi\Traits\ResponseTrait;
class LoginController extends Controller
{
use AuthenticatesUsers, ValidatesRequests, ResponseTrait;
/**
* @var CacheRepository
*/
private $cacheRepository;
/**
* @var VerifyEmailNotification
*/
private $verifyEmailNotification;
/**
* Create a new controller instance.
*
* @param CacheRepository $cacheRepository
* @param VerifyEmailNotification $verifyEmailNotification
*/
public function __construct(CacheRepository $cacheRepository, VerifyEmailNotification $verifyEmailNotification)
{
$this->middleware('guest')->except('logout');
$this->cacheRepository = $cacheRepository;
$this->verifyEmailNotification = $verifyEmailNotification;
}
/**
* {@inheritdoc}
*/
protected function attemptLogin(Request $request): bool
{
return $this->guard()->attempt($this->credentials($request));
}
/**
* Send the response after the user was authenticated.
*
* @param Request $request
* @return JsonResponse
*/
protected function sendLoginResponse(Request $request): JsonResponse
{
$user = $request->user();
try {
$this->checkUserIfIsActive($user, $request);
$this->checkIfUserHasVerifiedEmail($user, $request);
} catch (LockedException $exception) {
return $this->respondWithCustomData([
'message' => $exception->getMessage(),
], Response::HTTP_LOCKED);
}
$this->clearLoginAttempts($request);
$token = $user->createToken('TINNY-API Personal Access Client');
$expiration = Carbon::parse($token->token->expires_at)->toDateTimeString();
return $this->respondWithCustomData([
'access_token' => $token->accessToken,
'token_type' => 'Bearer',
'expires_at' => $expiration
]);
}
/**
* @param UserModel $user
* @param Request $request
*/
private function checkUserIfIsActive(UserModel $user, Request $request)
{
if (!$user->is_active) {
$this->logout($request);
$supportLink = config('support.support_url');
$message = __(
'Your account has been disabled, to enable it again, ' .
'please contact :support_link to start the process.',
['support_link' => '<a href="' . $supportLink . '">' . $supportLink . '</a>']
);
throw new LockedException($message);
}
}
/**
* Log the user out of the application.
*
* @param Request $request
* @return RedirectResponse|JsonResponse
*/
public function logout(Request $request)
{
$id = $this->guard()->id();
$this->cacheRepository->forget($id);
$this->cacheRepository->tags('users:' . $id)->flush();
$this->guard()->user()->token()->revoke();
$request->session()->invalidate();
$request->session()->regenerate();
return $request->wantsJson() ? $this->respondWithNoContent() : redirect('/');
}
/**
* @param UserModel $user
* @param Request $request
*/
private function checkIfUserHasVerifiedEmail(UserModel $user, Request $request)
{
if (!$user->hasVerifiedEmail()) {
Notification::send($user, $this->verifyEmailNotification->setToken($user->email_token_confirmation));
$this->logout($request);
$message = __(
'We sent a confirmation email to :email. Please follow the instructions to complete your registration.',
['email' => $user->email]
);
throw new LockedException($message);
}
}
}