change user login method in Laravel

2.3k views Asked by At

I'm editing a laravel project that use modules. I want change login field from email to mobile. In the user login controller in the login module, there are the following codes:

$loggedIn = $this->auth->login(
    [
        'email' => $request->email,
        'password' => $request->password,
    ],
    (bool) $request->get('remember_me', false)
);

I want user login with mobile, so I change the codes:

$loggedIn = $this->auth->login (
    [
        'mobile' => $request->email,
        'password' => $request->password,
    ],
    (bool) $request->get('remember_me', false)
);

But when I use this modified code, it does not change This means that users can still log in by entering an email! No mobile!

5

There are 5 answers

2
نور On BEST ANSWER

try this way...

source link

  if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
        // The user is being remembered...
    }
3
Nothehi On

add this lines to LoginController:

    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        return 'mobile';
    }
4
Kamlesh Paul On

you can use custom login like

$user = User::where('mobile', $request->email)->first();

if (Hash::check($request->password,$user->password)) {
    auth()->login($user, (bool) $request->get('remember_me', false));  // it will login that user 
}

ref link https://laravel.com/docs/8.x/authentication#other-authentication-methods

NOTE: in this way you have more control

0
Nurbek Boymurodov On
public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->mobile = $this->findMobile();
    }
    public function findMobile()
    {
        $login = request()->login;
        $fieldType = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'mobile';
        request()->merge([$fieldType => $login]);
        return $fieldType;
    }
    public function mobile()
    {
        return $this->mobile;
    }
    public function logout(Request $request) {
        Auth::logout();
        return redirect('/login');
    }

Try this with this code you can login with email or mobile

0
Emmanuel Iyen On

$customerInfo = array("email" => $email, "password" => $password);

   // $credentials = $request->only('email', 'password');
    if (Auth::attempt($customerInfo)) {
        return redirect()->intended('home')
                    ->withSuccess('You have Successfully loggedin');
    }