Merge pull request 'sayali' (#8) from sayali into main

Reviewed-on: Nikhil.Kadam/vib360#8
This commit is contained in:
2025-03-11 13:34:57 +00:00
4 changed files with 66 additions and 44 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace App\Http\Controllers\APIS;
namespace App\Http\Controllers\APIS\CustomerApi;
use App\Http\Controllers\Controller;
use App\Models\User;
@@ -10,60 +10,74 @@ use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\QueryException;
class AuthController extends Controller
{
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
]);
try {
// Validate incoming request data
$validator = Validator::make($request->all(), [
'email_address' => 'required|email',
'password' => 'required',
]);
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Login validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
// Check if validation failed
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Login validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
$credentials = [
'email_address' => $request->email_address,
'password' => $request->password,
];
// Check if the user is soft-deleted
$isDelete = User::where('email_address', $request->email_address)->onlyTrashed()->first();
if ($isDelete) {
return jsonResponseWithErrorMessageApi(__('auth.deleted_user_by_admin'), 403);
}
$isDelete = User::where('email_address', $request->email_address)->onlyTrashed()->first();
if ($isDelete) {
return jsonResponseWithErrorMessageApi(__('auth.deleted_user_by_admin'), 403);
}
$isExistEmail = User::where('email_address', $request->email_address)->whereNull('deleted_at')->first();
if ($isExistEmail == null) {
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email'), 403);
}
// Check if the user exists and is not soft-deleted
$isExistEmail = User::where('email_address', $request->email_address)->whereNull('deleted_at')->first();
if ($isExistEmail == null) {
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email'), 403);
}
if ($isExistEmail && !(Hash::check($request->password, $isExistEmail->password))) {
Log::error('Entered Password is wrong.');
return jsonResponseWithErrorMessageApi(__('auth.incorrect_password'), 403);
}
// Check if the entered password matches the stored password
if ($isExistEmail && !(Hash::check($request->password, $isExistEmail->password))) {
Log::error('Entered Password is wrong for ' . $request->email_address);
return jsonResponseWithErrorMessageApi(__('auth.incorrect_password'), 403);
}
if (Auth::attempt($credentials)) {
$user = Auth::user();
$token = JWTAuth::fromUser($user);
$response = [
'access_token' => $token,
'user' => $user,
// Attempt to authenticate the user
$credentials = [
'email_address' => $request->email_address,
'password' => $request->password,
];
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200);
if (Auth::attempt($credentials)) {
$user = Auth::user();
$token = JWTAuth::fromUser($user);
// Return success response with JWT token
$response = [
'access_token' => $token,
'user' => $user,
];
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200);
}
// Authentication failed
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 401);
} catch (QueryException $e) {
Log::error('Customer Login Failed: ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 401);
} catch (\Exception $e) {
Log::error('Unexpected error during login: ' . $e->getMessage());
return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500);
}
// Authentication failed
return response()->json(['error' => 'Unauthorized'], 401);
}

View File

@@ -27,7 +27,11 @@
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files": [
"app/Http/Helpers/Webhelper.php",
"app/Http/Helpers/Imagehelper.php"
]
},
"autoload-dev": {
"psr-4": {

View File

@@ -17,6 +17,8 @@ return new class extends Migration
$table->integer('tenant_id');
$table->uuid('customer_id');
$table->string('email')->unique();
$table->string('password')->nullable();
$table->string('authority', 50);
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
@@ -50,4 +52,4 @@ return new class extends Migration
Schema::dropIfExists('password_reset_tokens');
Schema::dropIfExists('sessions');
}
};
};

View File

@@ -3,8 +3,9 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\APIS\CustomerApi\UserAssetLinkController;
// use App\Http\Controllers\APIS\CustomerApi\AuthController;
// app\Http\Controllers\APIS\CustomerApi\AuthController.php
use App\Http\Controllers\APIS\CustomerApi\AuthController;
Route::get('/customerapi', function () {
return ('Welcome to admin api routes.');
});
@@ -12,3 +13,4 @@ Route::get('/customerapi', function () {
Route::get('/user-assets', [UserAssetLinkController::class, 'index']);
Route::post('/user-login', [AuthController::class, 'login']);