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!
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
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
try this way…
source link
if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
// The user is being remembered...
}