mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-02-23 14:57:46 +01:00
feat: ✨ Introduzione pagina di accesso e logica logout
This commit is contained in:
parent
c187aa8410
commit
2b7e7843ed
4
.idea/inspectionProfiles/Project_Default.xml
generated
4
.idea/inspectionProfiles/Project_Default.xml
generated
@ -103,7 +103,7 @@
|
||||
<inspection_tool class="HtmlUnknownTag" enabled="true" level="WARNING" enabled_by_default="true">
|
||||
<option name="myValues">
|
||||
<value>
|
||||
<list size="25">
|
||||
<list size="27">
|
||||
<item index="0" class="java.lang.String" itemvalue="nobr" />
|
||||
<item index="1" class="java.lang.String" itemvalue="noembed" />
|
||||
<item index="2" class="java.lang.String" itemvalue="comment" />
|
||||
@ -129,6 +129,8 @@
|
||||
<item index="22" class="java.lang.String" itemvalue="mwc-linear-progress" />
|
||||
<item index="23" class="java.lang.String" itemvalue="mwc-icon-button-toggle" />
|
||||
<item index="24" class="java.lang.String" itemvalue="slot" />
|
||||
<item index="25" class="java.lang.String" itemvalue="mwc-circular-progress" />
|
||||
<item index="26" class="java.lang.String" itemvalue="mwc-snackbar" />
|
||||
</list>
|
||||
</value>
|
||||
</option>
|
||||
|
71
app/Http/Controllers/AuthController.php
Normal file
71
app/Http/Controllers/AuthController.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
/**
|
||||
* Handle an authentication attempt.
|
||||
*/
|
||||
public function authenticate(Request $request): JsonResponse|Response
|
||||
{
|
||||
try {
|
||||
$request->validate($this->rules($request));
|
||||
} catch (ValidationException $e) {
|
||||
return response()->json(['errors' => $e->errors()], 422);
|
||||
}
|
||||
|
||||
$credentials = $request->only(['username', 'password']);
|
||||
|
||||
if (filter_var($request->get('username'), FILTER_VALIDATE_EMAIL)) {
|
||||
$credentials['email'] = $credentials['username'];
|
||||
unset($credentials['username']);
|
||||
}
|
||||
|
||||
if (auth()->attempt($credentials, $request->get('remember'))) {
|
||||
$request->session()->regenerate();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'errors' => ['invalid_credentials' => __('Le credenziali non sono valide.')],
|
||||
], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
#[ArrayShape(['username' => 'string', 'password' => 'string', 'remember' => 'string'])]
|
||||
private function rules(Request $request): array
|
||||
{
|
||||
$additional_validation = '';
|
||||
if (filter_var($request->input('username'), FILTER_VALIDATE_EMAIL)) {
|
||||
$additional_validation = '|email';
|
||||
}
|
||||
|
||||
return [
|
||||
'username' => 'required'.$additional_validation,
|
||||
'password' => 'required',
|
||||
'remember' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user out of the application.
|
||||
*
|
||||
* @noinspection RepetitiveMethodCallsInspection
|
||||
*/
|
||||
public function logout(Request $request): Response
|
||||
{
|
||||
auth()->logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
}
|
@ -11,9 +11,9 @@ class CreateUsersTable extends Migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('users', function (Blueprint $table) {
|
||||
Schema::create('users', static function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->string('username');
|
||||
$table->string('email')->unique();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
|
77
resources/js/Views/LoginPage.jsx
Normal file
77
resources/js/Views/LoginPage.jsx
Normal file
@ -0,0 +1,77 @@
|
||||
import '@maicol07/mwc-card';
|
||||
import '@material/mwc-checkbox';
|
||||
import '@material/mwc-formfield';
|
||||
import '@maicol07/mwc-layout-grid';
|
||||
import '../WebComponents/TextField';
|
||||
import type {Cash} from 'cash-dom';
|
||||
import redaxios from 'redaxios';
|
||||
|
||||
// eslint-disable-next-line import/no-absolute-path
|
||||
import logoUrl from '/images/logo_completo.png';
|
||||
|
||||
import LoadingButton from '../Components/LoadingButton.jsx';
|
||||
import Mdi from '../Components/Mdi.jsx';
|
||||
import Page from '../Components/Page.jsx';
|
||||
import {
|
||||
getFormData,
|
||||
showSnackbar
|
||||
} from '../utils';
|
||||
|
||||
export default class LoginPage extends Page {
|
||||
loading: Cash;
|
||||
|
||||
view(vnode) {
|
||||
return (
|
||||
<mwc-card outlined className="center ext-container ext-container-small">
|
||||
<img src={logoUrl} className="center stretch" alt={__('OpenSTAManager')}/>
|
||||
<form id="login" style="padding: 16px; text-align: center;">
|
||||
<h3 style="margin-top: 0;">{__('Accedi')}</h3>
|
||||
<text-field label={__('Nome utente/email')} id="username" name="username" style="margin-bottom: 16px;">
|
||||
<Mdi icon="account-outline" slot="icon"/>
|
||||
</text-field>
|
||||
<text-field label={__('Password')} id="password" name="password" type="password">
|
||||
<Mdi icon="lock-outline" slot="icon"/>
|
||||
</text-field>
|
||||
<mwc-formfield label={__('Ricordami')} style="display: block;">
|
||||
<mwc-checkbox id="remember" name="remember"/>
|
||||
</mwc-formfield>
|
||||
<LoadingButton raised id="login-button" label={__('Accedi')} icon="login-variant" style="float: right;"/>
|
||||
<mwc-button dense label="Password dimenticata" style="margin-top: 16px;">
|
||||
<Mdi icon="lock-question" slot="icon"/>
|
||||
</mwc-button>
|
||||
</form>
|
||||
</mwc-card>
|
||||
);
|
||||
}
|
||||
|
||||
oncreate(vnode) {
|
||||
super.oncreate(vnode);
|
||||
|
||||
this.loading = $(this.element).find('#login-button mwc-circular-progress');
|
||||
|
||||
$(this.element)
|
||||
.find('#login-button')
|
||||
.on('click', this.onLoginButtonClicked.bind(this));
|
||||
}
|
||||
|
||||
async onLoginButtonClicked() {
|
||||
this.loading.show();
|
||||
|
||||
const formData = getFormData($(this.element)
|
||||
.find('#login'));
|
||||
|
||||
formData._token = $('meta[name="csrf-token"]').attr('content');
|
||||
|
||||
try {
|
||||
await redaxios(window.route('auth.authenticate'), {
|
||||
method: 'POST',
|
||||
data: formData
|
||||
});
|
||||
} catch (error) {
|
||||
showSnackbar(Object.values(error.data.errors).join(' '), false);
|
||||
this.loading.hide();
|
||||
}
|
||||
|
||||
// Inertia.visit(window.route('dashboard'));
|
||||
}
|
||||
}
|
2
resources/js/Views/index.js
vendored
2
resources/js/Views/index.js
vendored
@ -1,4 +1,4 @@
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
|
||||
export { default as LoginPage } from './LoginPage.jsx';
|
||||
export { default as SetupPage } from './SetupPage.jsx';
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
/** @noinspection UnusedFunctionResultInspection */
|
||||
|
||||
use App\Http\Controllers\AuthController;
|
||||
use App\Http\Controllers\SetupController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
@ -16,15 +17,26 @@ use Illuminate\Support\Facades\Route;
|
||||
|
|
||||
*/
|
||||
|
||||
Route::get('/', function () {
|
||||
Route::get('/', static function () {
|
||||
if (empty(DB::connection()->getDatabaseName())) {
|
||||
return route('setup');
|
||||
return redirect()->route('setup');
|
||||
}
|
||||
|
||||
//return route('auth.login');
|
||||
return redirect()->route('auth.login');
|
||||
});
|
||||
|
||||
Route::name('auth.')->group(static function () {
|
||||
Route::inertia('login', 'LoginPage')
|
||||
->name('login');
|
||||
/*Route::inertia('password-request', '')
|
||||
->name('password-request');*/
|
||||
|
||||
Route::post('login', [AuthController::class, 'authenticate'])
|
||||
->name('authenticate');
|
||||
/*Route::post('logout', 'Auth\LoginController@logout')
|
||||
->name('auth.logout');*/
|
||||
});
|
||||
|
||||
// ----- PUBLIC ROUTES ----- //
|
||||
Route::inertia('setup', 'SetupPage', [
|
||||
'languages' => cache()->rememberForever('app.languages', fn () => array_map(
|
||||
static fn ($file) => basename($file, '.json'),
|
||||
@ -32,10 +44,15 @@ Route::inertia('setup', 'SetupPage', [
|
||||
)),
|
||||
'license' => cache()->rememberForever('app.license', fn () => file_get_contents(base_path('LICENSE'))),
|
||||
]);
|
||||
Route::options('setup/test', [SetupController::class, 'testDatabase'])->name('setup.test')->withoutMiddleware('csrf');
|
||||
Route::put('setup/save', [SetupController::class, 'save'])->name('setup.save');
|
||||
|
||||
Route::get('lang/{language}', function ($language) {
|
||||
Route::options('setup/test', [SetupController::class, 'testDatabase'])
|
||||
->name('setup.test')
|
||||
->withoutMiddleware('csrf');
|
||||
|
||||
Route::put('setup/save', [SetupController::class, 'save'])
|
||||
->name('setup.save');
|
||||
|
||||
Route::get('lang/{language}', static function ($language) {
|
||||
app()->setLocale($language);
|
||||
|
||||
return redirect()->back();
|
||||
|
Loading…
x
Reference in New Issue
Block a user