Files
backend_vib360_laravel/app/Http/Controllers/APIS/CustomerApi/AuthController.php

86 lines
3.1 KiB
PHP
Raw Normal View History

2025-03-11 16:51:14 +05:30
<?php
2025-03-11 19:03:30 +05:30
namespace App\Http\Controllers\APIS\CustomerApi;
2025-03-11 16:51:14 +05:30
use App\Http\Controllers\Controller;
2025-03-11 17:53:58 +05:30
use App\Models\User;
2025-03-11 16:51:14 +05:30
use Illuminate\Http\Request;
2025-03-11 17:53:58 +05:30
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
2025-03-11 19:03:30 +05:30
use Illuminate\Database\QueryException;
2025-03-11 16:51:14 +05:30
class AuthController extends Controller
{
2025-03-11 16:55:12 +05:30
2025-03-11 17:53:58 +05:30
public function login(Request $request)
{
2025-03-11 19:03:30 +05:30
try {
// Validate incoming request data
$validator = Validator::make($request->all(), [
'email_address' => 'required|email',
'password' => 'required',
]);
// Check if validation failed
if ($validator->fails()) {
$validationErrors = $validator->errors()->all();
Log::error("Login validation error: " . implode(", ", $validationErrors));
return jsonResponseWithErrorMessageApi($validationErrors, 403);
}
// 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);
}
// 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);
}
// 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);
}
// Attempt to authenticate the user
$credentials = [
'email_address' => $request->email_address,
'password' => $request->password,
];
2025-03-11 17:53:58 +05:30
2025-03-11 19:03:30 +05:30
if (Auth::attempt($credentials)) {
$user = Auth::user();
$token = JWTAuth::fromUser($user);
2025-03-11 17:53:58 +05:30
2025-03-11 19:03:30 +05:30
// Return success response with JWT token
$response = [
'access_token' => $token,
'user' => $user,
];
2025-03-11 17:53:58 +05:30
2025-03-11 19:03:30 +05:30
return jsonResponseWithSuccessMessage(__('auth.data_fetched_successfully'), $response, 200);
}
2025-03-11 17:53:58 +05:30
2025-03-11 19:03:30 +05:30
// Authentication failed
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 401);
2025-03-11 17:53:58 +05:30
2025-03-11 19:03:30 +05:30
} 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);
2025-03-11 17:53:58 +05:30
}
}
2025-03-11 16:55:12 +05:30
2025-03-11 16:51:14 +05:30
}
2025-03-11 16:55:12 +05:30