Answer the question
In order to leave comments, you need to log in
Laravel authorization/registration only by phone. How to fix validation and error output?
I decided to make authorization and registration only by phone.
I made this authorization form:
This is the registration:
This is the LoginController controller:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request) {
$phone = $request->phone;
$user = User::where('phone', $phone)->first();
if ($user) {
Auth::login($user);
return redirect()->route('catalog.index');
}
else {
return redirect()->back();
}
}
public function username() {
return 'phone';
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = RouteServiceProvider::HOME;
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
// 'name' => ['required', 'string', 'max:255'],
'phone' => ['required', 'numeric', 'min:11']
// 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
// 'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
protected function create(array $data)
{
return User::create([
'phone' => $data['phone'],
// 'name' => $data['name'],
// 'email' => $data['email'],
// 'password' => Hash::make($data['password']),
]);
}
}
Answer the question
In order to leave comments, you need to log in
I found a solution:
I thought that the validation was not performed at all (because min: 10 did not work), but it turns out that it is, but the validation itself had to be changed. I found some solution for checking Russian numbers, that's how it actually works.
'phone' => ['required', 'regex:/((8|\+7)-?)?\(?\d{3,5}\)?-?\d{1}-?\d {1}-?\d{1}-?\d{1}-?\d{1}((-?\d{1})?-?\d{1})?/', 'unique: users']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question