Skip to content

Commit

Permalink
fix responder and login
Browse files Browse the repository at this point in the history
  • Loading branch information
mazfreelance committed Oct 6, 2022
1 parent a9191c9 commit 8efc630
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
13 changes: 7 additions & 6 deletions app/Containers/v1/Authentication/Actions/LoginAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function isValidPassword(string $password, User $user): void
}
private function isOnline(User $user, bool $isForce): void
{
if (!$isForce && $user->online_status->value === UserOnlineStatus::Online) {
if ($user->online_status->value === UserOnlineStatus::Online && !$isForce) {
throw new GeneralHttpException(__('message.currently_online'));
}
}
Expand All @@ -63,17 +63,18 @@ private function isEmailVerified(User $user): void

private function getAccessToken(User $user, LoginDTO $loginDTO) : array
{

if (!config('app.allow_login_multiple_token')) {
$this->isOnline($user, $loginDTO->force);
$accessToken = $this->generatePersonalAccessToken($user);
} else $accessToken = $this->generatePasswordAccessToken($user, $loginDTO->password);

$user->update([
'online_status' => UserOnlineStatus::Online(),
'last_login_at' => Carbon::now(),
'last_login_ip' => Request::ip()
]);

if (!config('app.allow_login_multiple_token')) {
$this->isOnline($user, $loginDTO->force ?? true);
$accessToken = $this->generatePersonalAccessToken($user);
} else $accessToken = $this->generatePasswordAccessToken($user, $loginDTO->password);

return $accessToken;
}

Expand Down
2 changes: 1 addition & 1 deletion app/Containers/v1/Authentication/DTO/LoginDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function fromRequest(Request $request): self
return new self([
'email' => $request->email,
'password' => $request->password,
'force' => $request->force
'force' => $request->force ?? false
]);
}

Expand Down
7 changes: 4 additions & 3 deletions app/Ship/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Component\HttpKernel\Exception\{HttpException, MethodNotAllowedHttpException, ServiceUnavailableHttpException};
use Throwable;

class Handler extends ExceptionHandler
Expand Down Expand Up @@ -81,9 +80,11 @@ public function render($request, Throwable $exception)
return Responder::tooManyAttempts();
} else if ($exception instanceof ServiceUnavailableHttpException) {
return Responder::serverBusy();
} else if ($exception instanceof MethodNotAllowedHttpException) {
return Responder::methodNotAllowed();
} else if ($exception instanceof GeneralHttpException) {
return Responder::error($exception->getMessage(), $exception->getStatusCode());
} else {
}else {
if ($request->has('request_id')) {
RequestLog::where('request_id', $request->request_id)->update([
'response' => [
Expand Down
1 change: 1 addition & 0 deletions app/Ship/Support/Facades/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @method static \Illuminate\Http\JsonResponse forbiddenLogin()
* @method static \Illuminate\Http\JsonResponse tooManyAttempts()
* @method static \Illuminate\Http\JsonResponse collection(array $data = [])
* @method static \Illuminate\Http\JsonResponse methodNotAllowed()
*
* @see \App\Ship\Support\Responder
*/
Expand Down
13 changes: 13 additions & 0 deletions app/Ship/Support/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ public function serverBusy(): JsonResponse
], JsonResponse::HTTP_SERVICE_UNAVAILABLE);
}

/**
* Method Not Allowed
*
* @return JsonResponse
*/
public function methodNotAllowed(): JsonResponse
{
return response()->json([
'code' => JsonResponse::HTTP_METHOD_NOT_ALLOWED,
'message' => __('message.method_not_allowed'),
], JsonResponse::HTTP_NOT_FOUND);
}

/**
* Error
*
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'invalid_input' => 'The given data is invalid.',
'invalid_reset_token' => 'Invalid reset password token.',
'ip_not_whitelist' => 'Unauthorized ip address.',
'method_not_allowed' => 'Method not allowed.',
'no_approve_application' => 'No approved application found.',
'no_permission_access' => 'You do not have the permission to access this record.',
'no_permission_login' => 'You do not have the permission to login.',
Expand Down

0 comments on commit 8efc630

Please sign in to comment.