- Added AuthController for client login and token management. - Introduced AuthGate for handling authentication state and login UI. - Updated API to support fetching client data for pre-filling forms. - Modified App.vue to pre-fill client information if logged in. - Enhanced client model to include username and password fields. - Created migration for adding username and password to clients table. - Added utility for generating secure usernames and passwords. - Updated frontend to handle client tokens for pre-filling checkout forms. - Removed old CSS file and updated HTML references to new assets. - Added StatsOverview widget for displaying client and quote statistics. - Improved error handling and user feedback in the login process.
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Cliente;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
/**
|
|
* POST /api/auth/login
|
|
* Login del cliente per l'accesso al configuratore (username + password).
|
|
*/
|
|
public function login(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'username' => ['required', 'string'],
|
|
'password' => ['required', 'string'],
|
|
]);
|
|
|
|
$cliente = Cliente::where('username', $data['username'])->first();
|
|
|
|
if (! $cliente || ! $cliente->password || ! password_verify($data['password'], $cliente->password)) {
|
|
return response()->json([
|
|
'message' => 'Nome utente o password non validi.',
|
|
], 401);
|
|
}
|
|
|
|
$giorni = (int) config('preventivi.configurator_session_days', 30);
|
|
$token = $cliente->createToken('configuratore', ['*'], now()->addDays($giorni));
|
|
|
|
return response()->json([
|
|
'token' => $token->plainTextToken,
|
|
'expires_at' => $token->accessToken->expires_at,
|
|
'cliente' => [
|
|
'id' => $cliente->id,
|
|
'username' => $cliente->username,
|
|
'nominativo' => $cliente->nominativo,
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /api/auth/me
|
|
* Verifica che il token del configuratore sia ancora valido e restituisce i dati
|
|
* anagrafici del cliente, usati per precompilare il form di richiesta preventivo.
|
|
*/
|
|
public function me(Request $request): JsonResponse
|
|
{
|
|
$cliente = $request->user();
|
|
|
|
return response()->json([
|
|
'cliente' => [
|
|
'id' => $cliente->id,
|
|
'username' => $cliente->username,
|
|
'nominativo' => $cliente->nominativo,
|
|
'nome' => $cliente->nome,
|
|
'cognome' => $cliente->cognome,
|
|
'ragione_sociale' => $cliente->ragione_sociale,
|
|
'email' => $cliente->email,
|
|
'telefono' => $cliente->telefono,
|
|
'indirizzo' => $cliente->indirizzo,
|
|
'cap' => $cliente->cap,
|
|
'citta' => $cliente->citta,
|
|
'provincia' => $cliente->provincia,
|
|
'note' => $cliente->note,
|
|
],
|
|
]);
|
|
}
|
|
}
|