Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Zwracanie błędów logoowania - laravel.
Generic
post
Post #1





Grupa: Zarejestrowani
Postów: 224
Pomógł: 0
Dołączył: 31.10.2012

Ostrzeżenie: (10%)
X----


Cześć,

Wykorzystałem laravelowy mechanizm autoryzacji do logowania, który działa również z wykorzystaniem AJAX'a. Wszystko działa jednak chciałbym aby w funkcji sendFailedLoginResponse() błąd nie był ograniczony tylko do jednego dość ogólnego błędu, który sobie roboczo ustawiłem, a dostarczał konkretne błędy walidacji dla konkretnego pola email lub hasła. Czy ktoś mógłby mi powiedzieć jak to ugryźć i skąd mogę pobrać błędy walidacji?

Aktualnie moja funkcja wygląda następująco.

  1. protected function sendFailedLoginResponse()
  2. {
  3. return response()->json(['error' => 'Credentials invalid']);
  4. }


Ten post edytował Generic 22.03.2020, 09:15:47
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
Rysh
post
Post #2





Grupa: Zarejestrowani
Postów: 821
Pomógł: 111
Dołączył: 11.09.2006
Skąd: Biała Podlaska

Ostrzeżenie: (0%)
-----


Jeżeli chcesz błędy walidacji to użyj Requestów które wstrzykujesz w metodę kontrolera.

  1. <?php
  2.  
  3. namespace App\Http\Requests\Auth;
  4.  
  5. use Illuminate\Foundation\Http\FormRequest;
  6.  
  7. /**
  8.  * Class LoginRequest
  9.  */
  10. class LoginRequest extends FormRequest
  11. {
  12. /**
  13.   * Determine if the user is authorized to make this request.
  14.   *
  15.   * @return bool
  16.   */
  17. public function authorize(): bool
  18. {
  19. return true;
  20. }
  21.  
  22. /**
  23.   * Get the validation rules that apply to the request.
  24.   *
  25.   * @return array
  26.   */
  27. public function rules(): array
  28. {
  29. return [
  30. 'email' => 'required|string|email',
  31. 'password' => 'required|string',
  32. ];
  33. }
  34. }


i AuthController:

  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Http\Requests\Auth\LoginRequest;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Support\Facades\Auth;
  8.  
  9. /**
  10.  * Class AuthController
  11.  */
  12. class AuthController extends Controller
  13. {
  14. /**
  15.   * Login user and return a token.
  16.   *
  17.   * @param LoginRequest $request
  18.   *
  19.   * @return Response
  20.   */
  21. public function login(LoginRequest $request): Response
  22. {
  23. $credentials = $request->only('email', 'password');
  24.  
  25. if (!$token = Auth::guard()->attempt($credentials)) {
  26. return response(['message' => 'Invalid credentials.'], Response::HTTP_UNAUTHORIZED);
  27. }
  28.  
  29. return response(null, Response::HTTP_OK, ['Authorization' => $token]);
  30. }
  31. }
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 22:05