-
Notifications
You must be signed in to change notification settings - Fork 3
2. Login
dungda-0794 edited this page Feb 20, 2023
·
18 revisions
Note: Validation & logic login inside service
- Validate attributes
- Callback use model
- Generate JWT token
POST /login
- Parameters
| Request Field | Field Type | Required | Field Desc |
|---|---|---|---|
| username | string | true | username for user |
| password | string | true | password for user |
- Responses
| Response Field | Field Type | Required | Field Desc |
|---|---|---|---|
| refresh_token | string | true | refresh_token for user get access_token |
| access_token | string | true | access_token for user logged |
| token_type | string | true | token_type for user logged |
| expires_at | integer | true | expires_at for user logged |
curl -X 'POST' \
'http://localhost/api/login' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"username": "user1234",
"password": "passwordRequired@123"
}'{
"refresh_token": "eyJ0eXAiOiJKV1QiLC...",
"access_token": "eyJ0eXAiOiJKV1...",
"token_type": "bearer",
"expires_at": 1676281826
}Make route function the following:
# routes/api.php
Route::post('login', 'AuthController@login');Make login function the following:
# App\Http\Controllers\AuthController
public function login(Request $request)
{
$params = $request->only(['username', 'password']);
// use service package
$rs = $this->service->login($params, [], function ($entity) {
return $entity->only(['id', 'email', 'username']);
});
return response()->json($rs['auth']);
}
/**
* [login]
* @param array $credentials [The user's attributes for authentication.]
* @param array|null $attributes [The attributes use for query.]
* @param callable|null $callback [The callback function has the entity model.]
* @return [array]
*/
public function login(array $credentials = [], ?array $attributes = [], ?callable $callback = null): array;# SunAsterisk\Auth\Services\AuthJWTService;
public function login(array $credentials = [], ?array $attributes = [], ?callable $callback = null): array
{
$this->loginValidator($credentials)->validate();
...
}
...
protected function loginValidator(array $data)
{
return Validator::make($data, [
$this->username() => 'required',
$this->passwd() => 'required',
]);
}# SunAsterisk\Auth\Services\AuthJWTService;
public function login(array $credentials = [], ?array $attributes = [], ?callable $callback = null): array
{
...
$item = $this->repository->findByAttribute($attributes);
}# SunAsterisk\Auth\Services\AuthJWTService;
public function login(array $credentials = [], ?array $attributes = [], ?callable $callback = null): array
{
...
if (! $item || ! Hash::check(Arr::get($credentials, $this->passwd()), $item->{$this->passwd()})) {
throw ValidationException::withMessages([
'message' => $this->getFailedLoginMessage(),
]);
}
}# SunAsterisk\Auth\Services\AuthJWTService;
public function login(array $credentials = [], ?array $attributes = [], ?callable $callback = null): array
{
...
$payload = $this->jwt->make($itemArr)->toArray();
$payloadRefresh = $this->jwt->make($itemArr, true)->toArray();
$jwt = $this->jwt->encode($payload);
$refresh = $this->jwt->encode($payloadRefresh, true);
}Method login will return an array
'item' => $itemArr,
'auth' => [
'refresh_token' => 'eyJhbGciOiJIUzI1NiIsIn...',
'access_token' => 'eyJiwibmFtZSI6Ikpva...',
'token_type' => 'bearer',
'expires_at' => 1675742447,
],-
$itemArris array of object user model. We can custom by callback function as follows
# App\Http\Controllers\AuthController
$rs = $this->service->login($params, [], function ($entity) {
// Custom $itemArr
return $entity->only(['id', 'email', 'username']);
});BTW: Also we can change the sql query for the login flow as follows
# App\Http\Controllers\AuthController
$rs = $this->service->login(
$params,
[
'username' => $params['username'],
'is_active' => true, // custom query attributes
],
function ($entity) {
return $entity->only(['id', 'email', 'username']);
},
);