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-12 19:19:13 +05:30
|
|
|
// use App\Http\Controllers\APIS\CustomerApi\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
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 {
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
2025-03-11 19:20:18 +05:30
|
|
|
'email' => 'required|email',
|
2025-03-11 19:03:30 +05:30
|
|
|
'password' => 'required',
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
$validationErrors = $validator->errors()->all();
|
|
|
|
|
Log::error("Login validation error: " . implode(", ", $validationErrors));
|
|
|
|
|
return jsonResponseWithErrorMessageApi($validationErrors, 403);
|
|
|
|
|
}
|
2025-03-11 19:20:18 +05:30
|
|
|
$isExistEmail = User::where('email', $request->email)->first();
|
2025-03-11 19:03:30 +05:30
|
|
|
if ($isExistEmail == null) {
|
|
|
|
|
return jsonResponseWithErrorMessageApi(__('auth.incorrect_email'), 403);
|
|
|
|
|
}
|
|
|
|
|
if ($isExistEmail && !(Hash::check($request->password, $isExistEmail->password))) {
|
2025-03-11 19:20:18 +05:30
|
|
|
Log::error('Entered Password is wrong for ' . $request->email);
|
2025-03-11 19:03:30 +05:30
|
|
|
return jsonResponseWithErrorMessageApi(__('auth.incorrect_password'), 403);
|
|
|
|
|
}
|
|
|
|
|
$credentials = [
|
2025-03-11 19:20:18 +05:30
|
|
|
'email' => $request->email,
|
2025-03-11 19:03:30 +05:30
|
|
|
'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 19:20:18 +05:30
|
|
|
}
|
|
|
|
|
catch (QueryException $e) {
|
2025-03-11 19:03:30 +05:30
|
|
|
Log::error('Customer Login Failed: ' . $e->getMessage());
|
|
|
|
|
return jsonResponseWithErrorMessageApi(__('auth.authentication_failed'), 401);
|
2025-03-11 17:53:58 +05:30
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 19:19:13 +05:30
|
|
|
// public function login()
|
|
|
|
|
// {
|
|
|
|
|
// // Define the API endpoint
|
|
|
|
|
// $url = 'http://65.0.131.117:8080/api/auth/login';
|
|
|
|
|
|
|
|
|
|
// // Define the payload
|
|
|
|
|
// $payload = [
|
|
|
|
|
// 'username' => 'veoliauser@mail.com',
|
|
|
|
|
// 'password' => 'veolia123',
|
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
// // Make the POST request
|
|
|
|
|
// $response = Http::withHeaders([
|
|
|
|
|
// 'Content-Type' => 'application/json',
|
|
|
|
|
// ])->post($url, $payload);
|
|
|
|
|
|
|
|
|
|
// // Return the response
|
|
|
|
|
// return response()->json($response->json(), $response->status());
|
|
|
|
|
// }
|
2025-03-11 16:55:12 +05:30
|
|
|
|
|
|
|
|
|
2025-03-11 16:51:14 +05:30
|
|
|
}
|
2025-03-11 16:55:12 +05:30
|
|
|
|