I
I
Ivan2022-03-16 10:55:38
Laravel
Ivan, 2022-03-16 10:55:38

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:
6231976f2ae10241083005.png

This is the registration:
623197b071133434864420.png

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';
    }
}

Here is a RegisterController:
<?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']),
        ]);
    }
}

As you can see, the RegisterController has a validation that states that it is a minimum of 11 characters.
But the bottom line is that in the form I can specify at least 1 character and the user will be successfully created.

And on authorization simply does not come up with any errors. Those. if I specify a phone number that does not exist in the database (i.e. there is no such user), it will not authorize me, but it will not display any errors either. The page will refresh and that's it.

In the code, you may have noticed moments related to FireBase. I just confirm the number on it, but now it's not so important. For the time being, I removed it and simply did, roughly speaking, registration and authorization in one field - purely a phone number.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2022-03-16
@youmixx

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 question

Ask a Question

731 491 924 answers to any question