From 7a87491aa86edf4ab21e0dbcec9f36b61d543879 Mon Sep 17 00:00:00 2001 From: simba77 Date: Wed, 29 May 2024 17:12:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B4=D0=BC=D0=B8=D0=BD=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=D1=8F=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D1=80.=20=D0=94=D0=BB=D1=8F=20=D0=B0=D0=BF=D0=B8=20?= =?UTF-8?q?=D0=B0=D0=B4=D0=BC=D0=B8=D0=BD=D0=BA=D0=B8=20=D1=81=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D1=8B=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D1=80=D0=BE=D1=83=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 1 + composer.lock | 32 +++++++++---------- .../Users/Controllers/AdminAuthController.php | 12 ++----- modules/Users/DTO/AdminAuthFormRequestDTO.php | 17 ++++++++++ resources/admin/app.ts | 2 ++ resources/admin/composables/useAdminAuth.ts | 2 +- resources/admin/composables/useAsync.ts | 1 + resources/admin/pages/login.vue | 1 + resources/admin/stores/authStore.ts | 2 +- routes/admin-api.php | 11 +++++++ routes/web.php | 2 ++ 11 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 modules/Users/DTO/AdminAuthFormRequestDTO.php create mode 100644 routes/admin-api.php diff --git a/composer.json b/composer.json index f00ec6e..724fbd9 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ }, "autoload": { "psr-4": { + "App\\": "app/", "Modules\\": "modules/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" diff --git a/composer.lock b/composer.lock index 2edc1f2..933fb08 100644 --- a/composer.lock +++ b/composer.lock @@ -2060,16 +2060,16 @@ }, { "name": "laravel/framework", - "version": "v11.8.0", + "version": "v11.9.1", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "ceb892a25817c888ef3df4d1a2af9cac53978300" + "reference": "60167ce91c59ed5eea2ad4f2a7b6d686fb103ba7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/ceb892a25817c888ef3df4d1a2af9cac53978300", - "reference": "ceb892a25817c888ef3df4d1a2af9cac53978300", + "url": "https://api.github.com/repos/laravel/framework/zipball/60167ce91c59ed5eea2ad4f2a7b6d686fb103ba7", + "reference": "60167ce91c59ed5eea2ad4f2a7b6d686fb103ba7", "shasum": "" }, "require": { @@ -2261,20 +2261,20 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-05-21T17:57:45+00:00" + "time": "2024-05-28T18:16:41+00:00" }, { "name": "laravel/prompts", - "version": "v0.1.22", + "version": "v0.1.23", "source": { "type": "git", "url": "https://github.com/laravel/prompts.git", - "reference": "37f94de71758dbfbccc9d299b0e5eb76e02a40f5" + "reference": "9bc4df7c699b0452c6b815e64a2d84b6d7f99400" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/prompts/zipball/37f94de71758dbfbccc9d299b0e5eb76e02a40f5", - "reference": "37f94de71758dbfbccc9d299b0e5eb76e02a40f5", + "url": "https://api.github.com/repos/laravel/prompts/zipball/9bc4df7c699b0452c6b815e64a2d84b6d7f99400", + "reference": "9bc4df7c699b0452c6b815e64a2d84b6d7f99400", "shasum": "" }, "require": { @@ -2317,9 +2317,9 @@ "description": "Add beautiful and user-friendly forms to your command-line applications.", "support": { "issues": "https://github.com/laravel/prompts/issues", - "source": "https://github.com/laravel/prompts/tree/v0.1.22" + "source": "https://github.com/laravel/prompts/tree/v0.1.23" }, - "time": "2024-05-10T19:22:18+00:00" + "time": "2024-05-27T13:53:20+00:00" }, { "name": "laravel/sanctum", @@ -9760,16 +9760,16 @@ }, { "name": "spatie/ignition", - "version": "1.14.1", + "version": "1.14.2", "source": { "type": "git", "url": "https://github.com/spatie/ignition.git", - "reference": "c23cc018c5f423d2f413b99f84655fceb6549811" + "reference": "5e11c11f675bb5251f061491a493e04a1a571532" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/spatie/ignition/zipball/c23cc018c5f423d2f413b99f84655fceb6549811", - "reference": "c23cc018c5f423d2f413b99f84655fceb6549811", + "url": "https://api.github.com/repos/spatie/ignition/zipball/5e11c11f675bb5251f061491a493e04a1a571532", + "reference": "5e11c11f675bb5251f061491a493e04a1a571532", "shasum": "" }, "require": { @@ -9839,7 +9839,7 @@ "type": "github" } ], - "time": "2024-05-03T15:56:16+00:00" + "time": "2024-05-29T08:10:20+00:00" }, { "name": "spatie/laravel-ignition", diff --git a/modules/Users/Controllers/AdminAuthController.php b/modules/Users/Controllers/AdminAuthController.php index e4eb2fa..364d879 100644 --- a/modules/Users/Controllers/AdminAuthController.php +++ b/modules/Users/Controllers/AdminAuthController.php @@ -7,19 +7,13 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Response; +use Modules\Users\DTO\AdminAuthFormRequestDTO; class AdminAuthController { - public function login(Request $request): \Illuminate\Http\Response | array + public function login(AdminAuthFormRequestDTO $form, Request $request): \Illuminate\Http\Response | array { - $credentials = $request->validate( - [ - 'email' => ['required', 'email'], - 'password' => ['required'], - ] - ); - - if (Auth::attempt($credentials, (bool) $request->input('remember'))) { + if (Auth::attempt(['email' => $form->email, 'password' => $form->password], $form->remember)) { $request->session()->regenerate(); return ['id' => Auth::user()->id]; } else { diff --git a/modules/Users/DTO/AdminAuthFormRequestDTO.php b/modules/Users/DTO/AdminAuthFormRequestDTO.php new file mode 100644 index 0000000..4d46a98 --- /dev/null +++ b/modules/Users/DTO/AdminAuthFormRequestDTO.php @@ -0,0 +1,17 @@ + { function login(form: AuthLoginForm) { - return axios.post('/api/login', form) + return axios.post('/auth/login', form) } return { diff --git a/resources/admin/composables/useAsync.ts b/resources/admin/composables/useAsync.ts index cd9bb04..c6811cc 100644 --- a/resources/admin/composables/useAsync.ts +++ b/resources/admin/composables/useAsync.ts @@ -19,6 +19,7 @@ export default function useAsync unknown>(fn: T): const result = await fn(...args) return result as ReturnType } catch (error: any) { + validationErrors.value = null if (error?.response?.status === 422) { validationErrors.value = error.response.data } else { diff --git a/resources/admin/pages/login.vue b/resources/admin/pages/login.vue index 5d52680..6aa6193 100644 --- a/resources/admin/pages/login.vue +++ b/resources/admin/pages/login.vue @@ -31,6 +31,7 @@ const {loading, run: sendForm, validationErrors} = useAsync(() => login(form) .catch((response) => { if (response.response?.status === 401 && response.response?.data?.message) { error.value = response.response.data.message; + validationErrors.value = null } else { throw response } diff --git a/resources/admin/stores/authStore.ts b/resources/admin/stores/authStore.ts index 898ef41..385e1e8 100644 --- a/resources/admin/stores/authStore.ts +++ b/resources/admin/stores/authStore.ts @@ -10,7 +10,7 @@ export const authStore = defineStore('auth', { actions: { async checkAuth() { return new Promise((resolve, reject) => { - axios.get('/api/checkAuth') + axios.get('/auth/check-auth') .then((response) => { if (response.data.id !== null) { this.userData = response.data; diff --git a/routes/admin-api.php b/routes/admin-api.php new file mode 100644 index 0000000..d425323 --- /dev/null +++ b/routes/admin-api.php @@ -0,0 +1,11 @@ + 'auth'], function () { + Route::post('/login', [AdminAuthController::class, 'login'])->name('admin-auth.login'); + Route::get('/check-auth', [AdminAuthController::class, 'getCurrentAuth'])->name('admin-auth.currentAuth'); +}); diff --git a/routes/web.php b/routes/web.php index 311b1c6..e42545b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -5,6 +5,8 @@ use Illuminate\Support\Facades\Route; use Modules\Users\Controllers\AuthController; +Route::prefix('admin-api/v1')->group(base_path('routes/admin-api.php')); + Route::group(['prefix' => 'api'], function () { Route::post('/login', [AuthController::class, 'login'])->name('auth.login'); Route::get('/checkAuth', [AuthController::class, 'getCurrentAuth'])->name('auth.currentAuth');