User login authentication process is easy in laravel. User authentication process is already configured for you. laravel login or authentication configuration file is located at “app/config/auth.php” and a User model in “app/models” directory.
In this tutorial (Laravel user login authentication code example) i am sharing code or example for how to make a simple laravel user login authentication process using migrations, seeding. we will go through below steps to create a simple process.

Steps:-
1. Create an user table using migrations
2. Create an example user using seeding
3. Create login form, validate login, user login process using routes, controllers, and views
4. Getting logged in user data
5. Logout a user

1. Create an user table using migrations

For Laravel user login authentication code example process first we need to create a database setup then a user table and a sample user to login. for this create a database and set it up in “app/config/database.php” (setup which database driver you are using).

'mysql' => array(
	      'driver'    => 'mysql',
	      'host'      => 'localhost',
	      'database'  => 'laravel',
	      'username'  => 'root',
	      'password'  => '',
	      'charset'   => 'utf8',
	      'collation' => 'utf8_unicode_ci',
	      'prefix'    => '',
	   ),

After creating database we are going to create a user table using migrations.

Migrations :- migrations provide a way to modify the database schema and stay up to date on the current schema state. basically migrations are a way there we can manipulate database using code. we don’t need to use phpmyadmin to manipulate database we can diretly handle databse manipulation using our code.
Let’s create a users table using migrations for this go to your application root folder(where you have located “app” directory) and run below command.

php artisan migrate:make create_users_table --create=users

Example :-

root@127:/var/www/html/laravel# php artisan migrate:make create_users_table --create=users
(i have "app" directory in my laravel directory so my root will be "/var/www/html/laravel/")

Above command will create a new file in “app/database/migrations/date+create_users_table.php” will look like below. it’s a core migration file which will create a table with an id field and the timestamps fields (created_at and updated_at).

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
  public function up()
  {
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->timestamps();
    });
  }

/**
 * Reverse the migrations.
 *
 * @return void
 */
  public function down()
  {
       Schema::drop('users');
  }
}

Now we need to add some more fields to table using Schema Builder:-

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
  public function up()
  {
	Schema::create('users', function(Blueprint $table)
        {
          $table->increments('id');
          $table->string('name');
          $table->string('username');
          $table->string('email');
          $table->string('password');
          $table->string('remember_token')->nullable();
          $table->timestamps();
      });
  }

/**
 * Reverse the migrations.
 *
 * @return void
 */
  public function down()
  {
         Schema::drop('users');
  }
}

Now the migration file will create a table with given field for this we need to run this file using command line again so for create a table simple run this command:-

php artisan migrate

this command will create “users” table using up() function.

login1

2. Create an example user using seeding :-

Seeding:- Now we have users table with field and need to create a sample user for this we will use seeding for this. for this create a new file
in your “app/database/seeds” folder with named “UserTableSeeder.php”.

// app/database/seeds/UserTableSeeder.php
<?php

class UserTableSeeder extends Seeder
{
  public function run()
  {
    DB::table('users')->delete();
    
    User::create(array(
    	'name'     => 'Rakesh',
        'username' => 'Rakesh',
        'email'    => 'sharmarakesh395@gmail.com',
        'password' => Hash::make('mypass'),
    ));
  }
}

After create new seed file we need to do that laravel call it auto for this go to “app/database/seeds/DatabaseSeeder.php” and add the line $this->call(‘UserTableSeeder’); so file will be look like below :-

<?php

class DatabaseSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
  public function run()
  {
     Eloquent::unguard();
     $this->call('UserTableSeeder');
  }
}

After done seed now we need to call seed for this run the below command

php artisan db:seed

This will create a sample user with given credentials in users table. Now we are done with databse process and then need to views, controllers and routes.

Laravel user login authentication code example

3. Create login form, validate login, user login process using routes, controllers, and views :-

Routes :- first we need to setup routes for login. for this add below code in “app/routes.php”.

// GET route
Route::get('login', function() {
  return View::make('login');
});
//POST route
Route::post('login', 'AccountController@login');

Views :- Now we need to create a login form view file under “app/views/login.blade.php” and add below code to file.

<!doctype html>
<html>
<head>

</head>
<body>
{{ Form::open(array('url' => 'login')) }}
<h1>Login</h1>
@if(Session::has('error'))
<div class="alert-box success">
  <h2>{{ Session::get('error') }}</h2>
</div>
@endif
<div class="controls">
{{ Form::text('email','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Please Enter your Email')) }}
<p class="errors">{{$errors->first('email')}}</p>
</div>
<div class="controls">
{{ Form::password('password',array('class'=>'form-control span6', 'placeholder' => 'Please Enter your Password')) }}
<p class="errors">{{$errors->first('password')}}</p>
</div>
<p>{{ Form::submit('Login', array('class'=>'send-btn')) }}</p>
{{ Form::close() }}
</body>
</html>

Controllers:- create a file under “app/controllers” with named “AccountController.php” and add below code.

<?php
class AccountController extends BaseController {
  public function login() {
    // Getting all post data
    $data = Input::all();
    // Applying validation rules.
    $rules = array(
		'email' => 'required|email',
		'password' => 'required|min:6',
	     );
    $validator = Validator::make($data, $rules);
    if ($validator->fails()){
      // If validation falis redirect back to login.
      return Redirect::to('/login')->withInput(Input::except('password'))->withErrors($validator);
    }
    else {
      $userdata = array(
		    'email' => Input::get('email'),
		    'password' => Input::get('password')
		  );
      // doing login.
      if (Auth::validate($userdata)) {
        if (Auth::attempt($userdata)) {
          return Redirect::intended('/');
        }
      } 
      else {
        // if any error send back with message.
        Session::flash('error', 'Something went wrong'); 
        return Redirect::to('login');
      }
    }
  }
}

Now you are all set with example of laravel user login authentication. open browser and run url “http://mysite.com/login” and enter the sample user credentials and and submit and you are logged in to site

4. Getting logged in user data :- after logged in user you can get users data like

Access user information :-

$email = Auth::user()->email;
$id = Auth::id();

Check if a user is authenticated:-

if (Auth::check()) {
 // The user is logged in...
}

5. Logout a user :- for logout a user simple add below link where you want to all logout button

<a href="{{ URL::to('logout') }}">Logout</a>

Now set route for logout in “app/routes.php” :-

Route::get('logout', array('uses' => 'AccountController@logout'));

and add below function to your “app/controllers/AccountController.php”

public function logout() {
  Auth::logout(); // logout user
  return Redirect::to('login'); //redirect back to login
}

Now you are all done with Laravel user login authentication code example.