Introduction
In Laravel applications, user authentication is typically implemented using email and password combinations. However, as mobile usage increases, many applications now offer the option to log in using a phone number. This approach can enhance user experience by providing a more convenient and faster login method. In this article, we'll explore how to implement phone number login alongside email login in a Laravel application.
Prerequisites
Before diving into the implementation, ensure you have the following:
A Laravel application set up and running.
Basic understanding of Laravel's authentication system.
Access to a database to store user information.
Setting Up the Database
To support phone number logins, your users table must include a column for storing phone numbers. You can add this column using a migration:
php artisan make:migration add_phone_to_users_table --table=users
In the generated migration file, add the following:
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->unique()->nullable();
});
Run the migration to update your database:
php artisan migrate
Modifying the User Model
Next, update the User model to include the phone attribute in the $fillable array:
protected $fillable = [
'name', 'email', 'password', 'phone',
];
Customizing Authentication Logic
Laravel's authentication system is highly customizable. To allow login using either email or phone number, modify the LoginController to handle both cases.
Updating the Login Method
In LoginController, update the login method to check whether the input is an email or a phone number:
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
public function login(Request $request)
{
$input = $request->input('login');
$fieldType = filter_var($input, FILTER_VALIDATE_EMAIL) ? 'email' : 'phone';
$request->merge([$fieldType => $input]);
$this->validate($request, [
$fieldType => 'required',
'password' => 'required',
]);
if (Auth::attempt([$fieldType => $input, 'password' => $request->password])) {
return redirect()->intended('dashboard');
}
return back()->withErrors(['login' => 'Login details are not valid']);
}
Updating the Login Form
Modify your login form to include a single input field for both email and phone number. Update the form to use the name login for this input:
<form method="POST" action="{{ route('login') }}">
@csrf
<div class="form-group">
<label for="login">Email or Phone</label>
<input type="text" class="form-control" name="login" required autofocus>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
Testing the Implementation
With the changes in place, test the login functionality:
Register a new user with both an email and a phone number.
Attempt to log in using either the email or the phone number.
Ensure that the application redirects to the intended page upon successful login.
Conclusion
By implementing phone number login alongside email login in your Laravel application, you can offer users greater flexibility and convenience. This approach not only enhances the user experience but also aligns with modern authentication practices. As always, ensure that your application adheres to security best practices, such as encrypting sensitive data and using secure communication protocols.
Feel free to explore additional authentication methods, such as social logins or two-factor authentication, to further enhance your application's security and user experience.