Skip to content

Commit

Permalink
Админская авторизация вынесена в отдельный контроллер. Для апи админк…
Browse files Browse the repository at this point in the history
…и сделаны отдельные роуты
  • Loading branch information
simba77 committed May 29, 2024
1 parent f48398a commit 7a87491
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 27 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "modules/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
Expand Down
32 changes: 16 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 3 additions & 9 deletions modules/Users/Controllers/AdminAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions modules/Users/DTO/AdminAuthFormRequestDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Modules\Users\DTO;

use Spatie\LaravelData\Data;

class AdminAuthFormRequestDTO extends Data
{
public function __construct(
public string $email,
public string $password,
public bool $remember,
) {
}
}
2 changes: 2 additions & 0 deletions resources/admin/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
// Axios base api url
axios.defaults.baseURL = '/admin-api/v1'

import {createApp} from 'vue'
import {createPinia} from 'pinia'
Expand Down
2 changes: 1 addition & 1 deletion resources/admin/composables/useAdminAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import axios from "axios";

export const useAdminAuth = () => {
function login(form: AuthLoginForm) {
return axios.post('/api/login', form)
return axios.post('/auth/login', form)
}

return {
Expand Down
1 change: 1 addition & 0 deletions resources/admin/composables/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function useAsync<T extends (...args: any[]) => unknown>(fn: T):
const result = await fn(...args)
return result as ReturnType<T>
} catch (error: any) {
validationErrors.value = null
if (error?.response?.status === 422) {
validationErrors.value = error.response.data
} else {
Expand Down
1 change: 1 addition & 0 deletions resources/admin/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion resources/admin/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions routes/admin-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Modules\Users\Controllers\AdminAuthController;

Route::group(['prefix' => 'auth'], function () {
Route::post('/login', [AdminAuthController::class, 'login'])->name('admin-auth.login');
Route::get('/check-auth', [AdminAuthController::class, 'getCurrentAuth'])->name('admin-auth.currentAuth');
});
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 7a87491

Please sign in to comment.