diff --git a/app/Console/PipiCarInstallCommands.php b/app/Console/PipiCarInstallCommands.php index 46f7683..9e0386c 100644 --- a/app/Console/PipiCarInstallCommands.php +++ b/app/Console/PipiCarInstallCommands.php @@ -34,6 +34,7 @@ class PipiCarInstallCommands extends InstallCommand 'owners', // Владелец авто 'auto', // авто 'applications', //Заявки + 'pipi_users' // Добавление логики для пользователей ]; } diff --git a/app/Http/Controllers/MobileApiController.php b/app/Http/Controllers/MobileApiController.php index c5df13a..2d59aee 100644 --- a/app/Http/Controllers/MobileApiController.php +++ b/app/Http/Controllers/MobileApiController.php @@ -51,9 +51,25 @@ class MobileApiController extends Controller $data = $request->all(); $data['started_at'] = Carbon::parse($data['started_at'])?->format('Y-m-d H:i:s'); $data['ended_at'] = Carbon::parse($data['ended_at'])?->format('Y-m-d H:i:s'); - $data['user_id'] = UniModel::model('core_users') - ->where('email',$data['email'])->first()?->id; + $authToken = null; $data['car_id'] = null; + if ($request->header('Authorization')) { + $user = auth()->guard('api')->user(); + $data['user_id'] = $user ? $user->id : null; + } else { + $user = UniModel::model('core_users') + ->where('email', $data['email']) + ->first(); + + if ($user) { + $data['user_id'] = $user->id; + $tokenResult = $user->createToken('auth_token'); + $authToken = $tokenResult->accessToken; + } else { + $data['user_id'] = null; + } + + } UniModel::model('pipi_applications')->create([ 'rent_day' => $data['rent_day'], 'started_at' => $data['started_at'], @@ -67,14 +83,47 @@ class MobileApiController extends Controller 'status' => ApplicationStatus::pending->value ]); - if (!isset($data['user_id'])) { - return response()->json(['status' => 'OK', 'message' => 'Заявка создана, с вами свяжется наш оператор']); + $response = [ + 'status' => 'OK', + 'message' => 'Заявка создана', + ]; + + if ($authToken) { + $response['auth_token'] = $authToken; + $response['message'] = 'Заявка создана, и вы были автоматически авторизованы'; + } elseif (!isset($data['user_id'])) { + $response['message'] = 'Заявка создана, с вами свяжется наш оператор'; } - return response()->json(['status' => 'OK', 'message' => 'Заявка создана']); + + return response()->json($response); } public function getApplications(): JsonResponse { return response()->json(UniModel::model('pipi_applications')->get()); } + + public function login(Request $request) + { + $credentials = $request->only('email', 'password'); + + if (auth()->attempt($credentials)) { + $user = auth()->user(); + $tokenResult = $user->createToken('auth_token'); + + $response = [ + 'access_token' => $tokenResult->plainTextToken, + 'token_type' => 'Bearer', + 'user' => [ + 'id' => $user->id, + 'name' => $user->name, + 'email' => $user->email, + ], + ]; + + return response()->json($response, 200); + } else { + return response()->json(['error' => 'Unauthorized'], 401); + } + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index fc5f2e1..e28f2f7 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -40,7 +40,7 @@ class Kernel extends HttpKernel ], 'api' => [ - // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, + \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, \Illuminate\Routing\Middleware\ThrottleRequests::class.':api', \Illuminate\Routing\Middleware\SubstituteBindings::class, ], diff --git a/config/auth.php b/config/auth.php index 9548c15..1acfc20 100644 --- a/config/auth.php +++ b/config/auth.php @@ -40,6 +40,11 @@ return [ 'driver' => 'session', 'provider' => 'users', ], + + 'api' => [ + 'driver' => 'sanctum', + 'provider' => 'users', + ], ], /* diff --git a/modules/pipi_users/access.json b/modules/pipi_users/access.json new file mode 100644 index 0000000..9e4f179 --- /dev/null +++ b/modules/pipi_users/access.json @@ -0,0 +1,9 @@ +{ + "admin": [ + "default", + "add", + "show", + "edit", + "delete" + ] +} diff --git a/modules/pipi_users/app.json b/modules/pipi_users/app.json new file mode 100644 index 0000000..cbe1406 --- /dev/null +++ b/modules/pipi_users/app.json @@ -0,0 +1,8 @@ +{ + "module": "pipicar", + "name": "users", + "title": "Избранное", + "description": "'Учет.Главная'", + "type": "custom", + "class": "App.Modules.main.Applications.Main" +} diff --git a/modules/pipi_users/script.php b/modules/pipi_users/script.php new file mode 100644 index 0000000..03bd16d --- /dev/null +++ b/modules/pipi_users/script.php @@ -0,0 +1,40 @@ +upgrade(); + } + + private function upgrade(): void + { + $segments = Segment::listActive(); + foreach ($segments as $segment) { + if (!Schema::connection($segment->connector)->hasColumn('core_users', 'auth_token')) { + Schema::connection($segment->connector)->table('core_users', static function (Blueprint $table) { + $table->string('auth_token')->nullable()->unique(); + }); + } + if (!Schema::connection($segment->connector)->hasColumn('personal_access_tokens', 'expires_at')) { + Schema::connection($segment->connector)->table('personal_access_tokens', static function (Blueprint $table) { + $table->timestamp('expires_at')->nullable(); + }); + } + } + } +}; diff --git a/routes/api.php b/routes/api.php index 3401bf0..2ab7ddc 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,4 +26,5 @@ Route::prefix('mobile')->group(function () { Route::get('getMarks', [MobileApiController::class, 'getMarks']); Route::post('sendApplication', [MobileApiController::class, 'sendApplication']); Route::post('getApplications', [MobileApiController::class, 'getApplications']); + Route::post('login', [MobileApiController::class, 'login']); });