YouTube Icon

Code Playground.

How to Create Authentication Login in Laravel 5

CFG

How to Create Authentication Login in Laravel 5

In this guide, we will cover the Laravel 5 module authentication system. Laravel 5 offers a very solid core authentication system that enables simple authentication to be enforced. 

Steps For How to Create Authentication Login in Laravel 5

Step 1. Create route

Three routes are required. One is the login page view and the other is the post process submission form and the last is the user logout.

Route::get('/signin/', [
    'as'    => 'signin',
    'uses'  => 'SigninController@signinindex'
]);

Route::post('/signinlogin/', [
    'as'    => 'signinlogin',
    'uses'  => 'SigninController@signinlogin'
]);

Route::get('logout', [
    'as'    => 'logout',
    'uses'  => 'SigninController@Logoutuser'
]);

 Step 2. Login Page View

We will first need to import the Auth namespace

use Illuminate\Support\Facades\Auth;

In this step controller to load view page.

public function signinindex()
{

    if(Auth::check())
    {
        return redirect('/');
    }
    return view('signinindex');
}

signinindex.blade.php

@if(!empty(session('message')))
 <div class="alert alert-danger alert-dismissible fade in" role="alert">
      {{ session('message') }}
 </div>
@endif

<form  method="post" action="{{url('signinlogin')}}">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <label>Email Address </label>
  <input required=""  name="email"  type="email">
  <label>Your Password</label>
  <input  required="" name="password"  type="password">
  <button style="max-width: 100%;" name="button" type="submit" >Sign In</button>
</form>

Step 3. SigninController with signinlogin function

In this step form submit and go to route to controller and check authentication.

public function signinlogin(Request $request)
{

    $this->validate($request, array(
        'email' => 'required',
        'password' => 'required'
    ));

    if (Auth::attempt(['email' => $request->email, 'password' => $request->password],true))
    {
        return redirect()->intended('/');
    }
    else
    {
        return redirect()->back()->with('message', 'Email and Password is wrong!');
    }
}

Step 4. SigninController with Logoutuser function

In this step user logout code.

public function Logoutuser()
{
    Auth::logout();
    return redirect('signin');
}

And if you like this tutorials please share it with your friends via Email or Social Media.




CFG