-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoginController.php
144 lines (138 loc) · 4.2 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
<?php
namespace App\Http\Controllers\Api\V1\Auth;
use App\Http\Controllers\Api\V1\DingoController;
use App\Http\Transformers\NullObjectTransformer;
use App\Http\Transformers\UserTransformer;
use App\Models\NullObject;
use App\Models\User;
use Dingo\Api\Http\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginController extends DingoController
{
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
*/
public function login(Request $request)
{
$this->validateLogin($request);
$this->attemptLogin($request);
}
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$this->validate($request, [
$this->username() => 'required|string|email',
'password' => 'required|string',
]);
}
/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin(Request $request)
{
if (Auth::guard("web")->once($this->credentials($request)) ){
$this->sendLoginResponse($request);
} else {
$this->sendFailedLoginResponse($request);
}
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'password');
}
/**
* Send the response after the user was authenticated.
* The logic uses personal access token created by Passport
* in the background. Feel free to change it to your needs.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
protected function sendLoginResponse(Request $request)
{
$inputs = $request->all();
$user = User::where($this->username(), $inputs[$this->username()])->firstOrFail();
$meta = array(
'status_code' => 200,
'status_text' => "OK",
'message' => trans("auth.login.success"),
);
$response = $this->response->item($user, new UserTransformer())
->setStatusCode(200)
->setMeta($meta);
$response->throwResponse();
}
/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
}
/**
* Get the failed login response instance.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendFailedLoginResponse(Request $request)
{
$this->response()->errorUnauthorized(trans('auth.login.failed'));
}
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'email';
}
/**
* Log the user out of the application.
*
* The logout procedure just deletes the personal access token
* which was created by Passport. You can also just revoke them
* or incorporate refresh tokens. Do as you like.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$request->user()->token()->revoke();
$request->user()->token()->delete();
$meta = array(
'status_code' => 200,
'status_text' => "OK",
'message' => trans("auth.logout.success"),
);
$response = $this->response->item(new NullObject(), new NullObjectTransformer())
->setStatusCode(200)
->setMeta($meta);
// Use this method instead of send(). It also saves you from weird
// assertJsonStructure errors
$response->throwResponse();
}
}