refactor: unfuck register request
This commit is contained in:
parent
e28348cfbf
commit
6722c9a75d
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\RegisterRequest;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Hash;
|
use Hash;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
@ -10,9 +11,9 @@ use Validator;
|
||||||
|
|
||||||
class PublicUserController extends Controller
|
class PublicUserController extends Controller
|
||||||
{
|
{
|
||||||
public function register(Request $request)
|
public function register(RegisterRequest $request)
|
||||||
{
|
{
|
||||||
$data = $request->all()['user'];
|
$data = $request->all();
|
||||||
if (User::where([ 'email' => $data['email'] ])->count() != 0) {
|
if (User::where([ 'email' => $data['email'] ])->count() != 0) {
|
||||||
return response()
|
return response()
|
||||||
->json('email_taken', 400);
|
->json('email_taken', 400);
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Rules\ZxcvbnRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
|
class RegisterRequest extends RestRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'last_name' => ['required', 'string'],
|
||||||
|
'name' => ['required', 'string'],
|
||||||
|
'middle_name' => ['required', 'string'],
|
||||||
|
'email' => ['required', 'email'],
|
||||||
|
'phone' => ['required', 'regex:/^\+\d+$/'],
|
||||||
|
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\Validator;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||||
|
|
||||||
|
class RestRequest extends FormRequest
|
||||||
|
{
|
||||||
|
protected function failedValidation(Validator $validator)
|
||||||
|
{
|
||||||
|
$exception = $validator->getException();
|
||||||
|
|
||||||
|
throw new HttpResponseException(response()->json($validator->errors(), 422));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use ZxcvbnPhp\Zxcvbn;
|
||||||
|
|
||||||
|
class ZxcvbnRule implements ValidationRule
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the validation rule.
|
||||||
|
*
|
||||||
|
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
|
||||||
|
*/
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
$value = (string) $value;
|
||||||
|
$zxcvbn = new Zxcvbn();
|
||||||
|
if ($zxcvbn->passwordStrength($value)['score'] < 3) {
|
||||||
|
$fail('Password is not secure enough!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,8 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.2",
|
"php": "^8.2",
|
||||||
|
"bjeavons/zxcvbn-php": "^1.3",
|
||||||
|
"egulias/email-validator": "^4.0",
|
||||||
"laravel/framework": "^11.9",
|
"laravel/framework": "^11.9",
|
||||||
"laravel/tinker": "^2.9"
|
"laravel/tinker": "^2.9"
|
||||||
},
|
},
|
||||||
|
|
|
@ -4,8 +4,63 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "7e8c3c14ff33b199b4a0838993eb8423",
|
"content-hash": "847793c6725e2d2fecbf33402e6d4c4d",
|
||||||
"packages": [
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "bjeavons/zxcvbn-php",
|
||||||
|
"version": "1.3.1",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/bjeavons/zxcvbn-php.git",
|
||||||
|
"reference": "994928ae5b17ecff8baa2406832d37bdf01116c0"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/bjeavons/zxcvbn-php/zipball/994928ae5b17ecff8baa2406832d37bdf01116c0",
|
||||||
|
"reference": "994928ae5b17ecff8baa2406832d37bdf01116c0",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-json": "*",
|
||||||
|
"php": "^7.2 | ^8.0 | ^8.1",
|
||||||
|
"symfony/polyfill-mbstring": ">=1.3.1"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"php-coveralls/php-coveralls": "*",
|
||||||
|
"phpunit/phpunit": "^8.5",
|
||||||
|
"squizlabs/php_codesniffer": "3.*"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-gmp": "Required for optimized binomial calculations (also requires PHP >= 7.3)"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"ZxcvbnPhp\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "See contributors",
|
||||||
|
"homepage": "https://github.com/bjeavons/zxcvbn-php"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Realistic password strength estimation PHP library based on Zxcvbn JS",
|
||||||
|
"homepage": "https://github.com/bjeavons/zxcvbn-php",
|
||||||
|
"keywords": [
|
||||||
|
"password",
|
||||||
|
"zxcvbn"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/bjeavons/zxcvbn-php/issues",
|
||||||
|
"source": "https://github.com/bjeavons/zxcvbn-php/tree/1.3.1"
|
||||||
|
},
|
||||||
|
"time": "2021-12-21T18:37:02+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "brick/math",
|
"name": "brick/math",
|
||||||
"version": "0.12.1",
|
"version": "0.12.1",
|
||||||
|
|
Loading…
Reference in New Issue