How To Make Login Page In Laravel

Creating a login page is an essential aspect of web development. In this article, I will guide you through the process of making a login page in Laravel, one of the most popular PHP frameworks.

Setting up Laravel

First, let’s make sure you have Laravel installed on your system. If not, head over to the Laravel website and follow the installation instructions. Once Laravel is installed, open your terminal and navigate to your project directory.

$ cd path/to/your/project

Now, let’s generate the authentication scaffolding using Laravel’s built-in Artisan command:

$ php artisan make:auth

This command will create the necessary views, routes, and controllers for authentication. It will also generate a migration file for the users table.

Database Setup

Before moving forward, let’s configure our database connection. Open the .env file in your project root directory and set the appropriate values for your database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Note: Replace “your_database_name”, “your_database_username”, and “your_database_password” with your actual database credentials.

Next, run the following command to migrate the users table:

$ php artisan migrate

This will create the necessary table in your database.

Creating the Login Page

Now that the setup is complete, let’s move on to creating the login page. Open the resources/views/auth/login.blade.php file. This file contains the HTML markup for the login form.

Laravel provides a convenient way to generate the login form using Blade templating engine. You can use the {{ csrf_field() }} method to generate a hidden CSRF token field, which is required for security purposes. Additionally, you can use the @error directive to display any validation errors.

Here’s an example of a basic login form:

<form method="POST" action="{{ route('login') }}">
@csrf

<div class="form-group">
<label for="email">Email Address</label>
<input id="email" type="email" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>

<div class="form-group">
<label for="password">Password</label>
<input id="password" type="password" name="password" required autocomplete="current-password">
@error('password')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>

<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="remember" name="remember" {{ old('remember') ? 'checked' : '' }}>
<label class="form-check-label" for="remember">
Remember Me
</label>
</div>
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">
Login
</button>
</div>

<div class="form-group mb-0">
<a href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</form>

Feel free to modify the form as per your requirements, adding additional fields or styling as needed.

Routing and Controller Logic

Now let’s move on to the routing and controller logic for handling the login request. Open the routes/web.php file and add the following routes:

Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('/login', 'Auth\LoginController@login');
Route::post('/logout', 'Auth\LoginController@logout')->name('logout');

These routes map the login form to the appropriate controller methods. Now, open the app/Http/Controllers/Auth/LoginController.php file and add the following code:

use AuthenticatesUsers;

protected $redirectTo = '/home';

public function __construct()
{
$this->middleware('guest')->except('logout');
}

The AuthenticatesUsers trait provides the necessary methods for handling user authentication. You can customize the $redirectTo property to specify the page users will be redirected to after successful login.

Conclusion

Creating a login page in Laravel is a straightforward process. By following the steps outlined in this article, you should now have a fully functional login page for your Laravel application. Remember to test your login functionality thoroughly and make any necessary adjustments to fit your specific requirements.

If you want to learn more about Laravel’s authentication features, I recommend checking out the official Laravel documentation at https://laravel.com/docs/authentication.

Happy coding!