Login with Email or Phone or Username

640 0 0 0

Last Updated : 2024-04-24 15:18:32

In this snippet I will teach you how to make login process in laravel using email or phone number or username

So in this snippet I will teach you how to make login process easy with email or phone or username like the code below


public function adminLogin(Request $request) {
// validate form data
$this->validate($request, [
'emailOrPhoneOrUsername' => 'required',
'password' => 'required',
]);

// if the input is numeric (Phone)
if(is_numeric($request->get('emailOrPhoneOrUsername'))){
if (Auth::guard('admin')->attempt(['phone' => $request->emailOrPhoneOrUsername, 'password' => $request->password], $request->remember)) {
return redirect()->intended( route('admin.home') );
}
} elseif (filter_var($request->get('emailOrPhoneOrUsername'), FILTER_VALIDATE_EMAIL)) {
if (Auth::guard('admin')->attempt(['email' => $request->emailOrPhoneOrUsername, 'password' => $request->password], $request->remember)) {
return redirect()->intended( route('admin.home') );
}
} else {
if (Auth::guard('admin')->attempt(['username' => $request->emailOrPhoneOrUsername, 'password' => $request->password], $request->remember)) {
return redirect()->intended( route('admin.home') );
}
}

// if unsuccessful , then redirect back to the login with the form data
return redirect()->back()->withInput($request->only('emailOrPhoneOrUsername', 'remember'))->with('adminLoginMSG', 'These credentials do not match our records');
}

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.