Skip to content

Commit

Permalink
auth login and logout api working
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Sep 28, 2023
1 parent 819cf10 commit c008d80
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 48 deletions.
37 changes: 37 additions & 0 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,41 @@
| This value will be used to across system where model is needed
*/
'user_profile_model' => \Fintech\Auth\Models\Profile::class,

/*
|--------------------------------------------------------------------------
| Login Validation
|--------------------------------------------------------------------------
|
| This value will be used to across system where model is needed
*/
'validation' => [
'login' => [
'login_id' => ['required', 'string'],
'password' => ['required', 'string', \Illuminate\Validation\Rules\Password::default()],
]
],

/*
|--------------------------------------------------------------------------
| Lock Up Threshold
|--------------------------------------------------------------------------
|
| This value will be used to across system where model is needed
*/
'threshold' => [
'password' => 10,
'pin' => 3,
],

'threshold_notification' => false,

/*
|--------------------------------------------------------------------------
| Authentication Middleware
|--------------------------------------------------------------------------
|
| This value will be used to across system where model is needed
*/
'middleware' => ['auth:sanctum']
];
Empty file removed lang/.gitkeep
Empty file.
28 changes: 28 additions & 0 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/*
|--------------------------------------------------------------------------
| Meta Data Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during authentication for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/
return [
'success' => 'Login successful.',
'logout' => 'Logout successful. Thank you for using our services',
'failed' => 'These credentials do not match our records.',
'password' => 'The provided password is incorrect.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'Invalid Token' => 'Invalid Token',
'Your IP :user_ip is blocked. Please contact support.' => 'Your IP :user_ip is blocked. Please contact support.', //don't translate :user_ip
'This user are not login. Please contact support.' => 'This user are not login. Please contact support.',
'Sorry, You entered wrong mobile number or invalid password!' => 'Sorry, You entered wrong mobile number or invalid password!',
'warning' => 'Sorry, You entered wrong credentials! You already attempt :attempt. times out of :threshold',
'lockup' => 'Sorry, Your Account is has been Locked. Please contact support!',
'This user are not login' => 'This user are not login',
'Sorry, You entered wrong mobile number or invalid pin!' => 'Sorry, You entered wrong mobile number or invalid pin!',
'Sorry, You entered wrong mobile number or pin! You already attempt :wrong_pin_password. times out of :password_retry_limit'
=> 'Sorry, You entered wrong mobile number or pin! You already attempt :wrong_pin_password. times out of :password_retry_limit',
];
19 changes: 8 additions & 11 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@
->name('verification.send');

Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])
->middleware('auth')
->middleware(config('fintech.auth.middleware'))
->name('logout');

Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class);
// Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class);
// Route::apiResource('permissions', \Fintech\Auth\Http\Resources\PermissionCollection::class);
// Route::apiResource('teams', \Fintech\Auth\Http\Controllers\TeamController::class);
Route::apiSingleton('users.profile', \Fintech\Auth\Http\Controllers\ProfileController::class);
Route::middleware(config('fintech.auth.middleware'))->group(function () {
Route::apiResource('users', \Fintech\Auth\Http\Controllers\UserController::class);
// Route::apiResource('roles', \Fintech\Auth\Http\Controllers\RoleController::class);
// Route::apiResource('permissions', \Fintech\Auth\Http\Resources\PermissionCollection::class);
// Route::apiResource('teams', \Fintech\Auth\Http\Controllers\TeamController::class);
Route::apiSingleton('users.profile', \Fintech\Auth\Http\Controllers\ProfileController::class);
});
});
});

Route::prefix('v2')->group(function () {
Route::prefix('auth')->group(function () {

});
});
12 changes: 12 additions & 0 deletions src/Enums/UserStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Fintech\Auth\Enums;

enum UserStatus: string
{
case Active = 'ACTIVE';
case InActive = 'IN-ACTIVE';
case Banned = 'BANNED';
case Flagged = 'FLAGGED';
case Terminated = 'TERMINATED';
}
59 changes: 40 additions & 19 deletions src/Http/Controllers/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Fintech\Auth\Http\Controllers;

use Fintech\Auth\Enums\UserStatus;
use Fintech\Auth\Http\Requests\LoginRequest;
use Fintech\Auth\Models\User;
use Fintech\Auth\Http\Resources\LoginResource;
use Fintech\Core\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class AuthenticatedSessionController extends Controller
Expand All @@ -20,43 +20,64 @@ class AuthenticatedSessionController extends Controller
* Handle an incoming authentication request.
*
* @param LoginRequest $request
* @return JsonResponse
* @return LoginResource|JsonResponse
* @throws ValidationException
*/
public function store(LoginRequest $request): JsonResponse
public function store(LoginRequest $request): LoginResource|JsonResponse
{
$request->ensureIsNotRateLimited();

if (! Auth::attempt($request->only('login_id', 'password'))) {
$attemptUser = \Fintech\Auth\Facades\Auth::user()->list([
'login_id' => $request->input('login_id'),
'paginate' => false
]);

if ($attemptUser->isEmpty()) {

return $this->failed(__('auth::messages.failed'));
}

$attemptUser = $attemptUser->first();

if ($attemptUser->wrong_password > config('fintech.auth.threshold.password', 10)) {

\Fintech\Auth\Facades\Auth::user()->update($attemptUser->id, [
'status' => UserStatus::InActive->value
]);

return $this->failed(__('auth::messages.lockup'));
}

if (!Hash::check($request->input('password'), $attemptUser->password)) {

$request->hitRateLimited();

return $this->failed(__('auth.failed'));
\Fintech\Auth\Facades\Auth::user()->update($attemptUser->id, [
'wrong_password' => $attemptUser->wrong_password + 1
]);

return $this->failed(__('auth::messages.failed'));
}

$request->clearRateLimited();

/**
* @var User $authUser
*/
$authUser = Auth::user();
Auth::login($attemptUser);

Auth::user()->tokens->each(fn($token) => $token->delete());

$token = $authUser->createToken(config('app.name'))->plainTextToken;
//permission check

return response()->json(['data' => $authUser, 'token' => $token, 'message' => 'Login Successful.'], Response::HTTP_OK);
return new LoginResource(Auth::user());
}

/**
* Destroy an authenticated session.
* @return JsonResponse
*/
public function destroy(Request $request): Response
public function destroy(): JsonResponse
{
Auth::guard('web')->logout();

$request->session()->invalidate();

$request->session()->regenerateToken();

return response()->noContent();
return $this->deleted(__('auth::messages.logout'));
}
}
6 changes: 3 additions & 3 deletions src/Http/Requests/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function authorize(): bool
*/
public function rules(): array
{
return [
return config('fintech.auth.validation.login', [
'login_id' => ['required', 'string'],
'password' => ['required', 'string'],
];
'password' => ['required', 'string', \Illuminate\Validation\Rules\Password::default()],
]);
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Http/Resources/LoginResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Fintech\Auth\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class LoginResource extends JsonResource
{

/**
* Transform the resource into an array.
*
* @param Request
* @return array
*/
public function toArray($request)
{
$this->resource->load([
'profile.country', 'profile.state', 'profile.city',
'profile.presentCountry', 'profile.presentState', 'profile.presentCity'
]);

return [
'id' => $this->id ?? null,
'name' => $this->name ?? null,
'mobile' => $this->mobile ?? null,
'email' => $this->email ?? null,
'login_id' => $this->login_id ?? null,
'status' => $this->status ?? null,
'language' => $this->language ?? null,
'currency' => $this->currency ?? null,
'app_version' => $this->app_version ?? null,
'total_balance' => 0,
'email_verified_at' => $this->email_verified_at ?? null,
'mobile_verified_at' => $this->mobile_verified_at ?? null,
'created_at' => $this->created_at ?? null,
'updated_at' => $this->updated_at ?? null,
'profile' => (($this->profile != null)
? [
'user_profile_data' => $this->profile->user_profile_data ?? null,
'id_type' => $this->profile->id_type ?? null,
'id_no' => $this->profile->id_no ?? null,
'id_issue_country' => $this->profile->id_issue_country ?? null,
'id_expired_at' => $this->profile->id_expired_at ?? null,
'id_issue_at' => $this->profile->id_issue_at ?? null,
'id_no_duplicate' => $this->profile->id_no_duplicate ?? null,
'date_of_birth' => $this->profile->date_of_birth ?? null,
'address' => $this->profile->permanent_address ?? null,
'city_id' => $this->profile->city_id ?? null,
'city_name' => $this->profile->city->name ?? null,
'state_id' => $this->profile->state_id ?? null,
'state_name' => $this->profile->state->name ?? null,
'country_id' => $this->profile->country_id ?? null,
'country_name' => $this->profile->country->name ?? null,
'post_code' => $this->profile->post_code ?? null,
'present_address' => $this->profile->present_address ?? null,
'present_city_id' => $this->profile->present_city_id ?? null,
'present_city_name' => $this->profile->presentCity->name ?? null,
'present_state_id' => $this->profile->present_state_id ?? null,
'present_state_name' => $this->profile->presentState_name ?? null,
'present_country_id' => $this->profile->present_country_id ?? null,
'present_country_name' => $this->profile->presentCountry_name ?? null,
'present_post_code' => $this->profile->present_post_code ?? null,

'blacklisted' => $this->profile->blacklisted ?? null,
'created_at' => $this->profile->created_at ?? null,
'updated_at' => $this->profile->updated_at ?? null,
]
: (new \stdClass()))
];
}

/**
* Get additional data that should be returned with the resource array.
*
* @param Request $request
* @return array<string, mixed>
*/
public function with(Request $request): array
{
$origin = Str::slug(config('app.name'));

return [
'access' => [
'token' => $this->createToken($origin)->plainTextToken,
'type' => 'bearer',
'permissions' => [
'login',
'dashboard'
]
],
'message' => trans('auth::messages.success')
];
}
}
Loading

0 comments on commit c008d80

Please sign in to comment.