Skip to content

Commit

Permalink
LP-3 login with mobile email and login_id function added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Oct 21, 2023
1 parent cbe74db commit 5c85616
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Http/Controllers/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Fintech\Auth\Events\AccountFreezed;
use Fintech\Auth\Http\Requests\LoginRequest;
use Fintech\Auth\Http\Resources\LoginResource;
use Fintech\Auth\Traits\GuessAuthFieldTrait;
use Fintech\Core\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
Expand All @@ -16,6 +17,7 @@
class AuthenticatedSessionController extends Controller
{
use ApiResponseTrait;
use GuessAuthFieldTrait;

/**
* Handle an incoming authentication request.
Expand All @@ -28,11 +30,7 @@ public function store(LoginRequest $request): LoginResource|JsonResponse
{
$request->ensureIsNotRateLimited();

$authField = config('fintech.auth.auth_field', 'login_id');

$attemptUser = \Fintech\Auth\Facades\Auth::user()->list([
$authField => $request->input($authField)
]);
$attemptUser = \Fintech\Auth\Facades\Auth::user()->list($this->getAuthFieldFromInput($request));

if ($attemptUser->isEmpty()) {

Expand Down Expand Up @@ -80,7 +78,7 @@ public function store(LoginRequest $request): LoginResource|JsonResponse

Auth::login($attemptUser);

$attemptUser->tokens->each(fn ($token) => $token->delete());
$attemptUser->tokens->each(fn($token) => $token->delete());

return new LoginResource($attemptUser);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Traits/GuessAuthFieldTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Fintech\Auth\Traits;


trait GuessAuthFieldTrait
{
/**
* Return user model auth field from input format
*
* @param $request
* @return array
*/
private function getAuthFieldFromInput($request)
{
$authFieldValue = $request->input(config('fintech.auth.auth_field', 'login_id'));

if (filter_var($authFieldValue, FILTER_VALIDATE_EMAIL)) {
return ['email' => $authFieldValue];
} elseif (preg_match('/^(\d{7,15})$/', $authFieldValue) > 0) {
return ['mobile' => $authFieldValue];
} else {
return [config('fintech.auth.auth_field', 'login_id') => $authFieldValue];
}
}
}

0 comments on commit 5c85616

Please sign in to comment.