Home > Mobile >  Laravel UI login user with another field
Laravel UI login user with another field

Time:07-20

I use Laravel UI for authentication. In my user table, I have a field called telephone. Can I use to log in the user with that telephone and password with Laravel UI Authentication? I tried to change the contents in App/Http/Controllers/Auth/LoginController.php but couldn't figure out how this works. Really appreciate it if somebody could help. Thanks.

App/Http/Controllers/Auth/LoginController.php,

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::REDIRECT;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

CodePudding user response:

You can put this on your LoginController to override the Laravel default.

public function username()
{
    return 'telephone';
}

CodePudding user response:

By adding this function in your App\Http\Controllers\LoginController.php, You can override the login method

    public function login(Request $request)
    {
           $request->validate([
                'mobile' => 'required',
                'password' => 'required',
            ]);

            $credentials = $request->only('mobile', 'password');
            if (Auth::attempt($credentials)) {
                URL::to('home');
            }

            return redirect("login")->withSuccess('Oppes! You have entered invalid credentials');
        
     }

CodePudding user response:

If you are using fortify

edit config/fortify.php

'username' => 'telephone',

in the username key replace the value with the field which you want for login

CodePudding user response:

By default, Laravel uses the email field for authentication. If you would like to customize this, you may override username method on your LoginController:

public function username()
{
    return 'telephone';
}
  • Related