Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

How to Create a Simple Login Authentication System in LARAVEL?Part-2

How to Create a Simple Login Authentication System in LARAVEL?

Firstly, Visit this Blog Part-1.

Step 15. Now add these classes within MainController file.

use Validator;
use Auth;

Step 16. Create a function checklogin to receive login form request within the MainController file.

function checklogin(Request $request)
    {
     $this->validate($request, [
      'email'   => 'required|email',
      'password'  => 'required|alphaNum|min:3'
     ]);

     $user_data = array(
      'email'  => $request->get('email'),
      'password' => $request->get('password')
     );

     if(Auth::attempt($user_data))
     {
      return redirect('main/successlogin');
     }
     else
     {
      return back()->with('error', 'Wrong Login Details');
     }

    }

Step 17. Write down the following code within the login form file for validation error:


   @if (count($errors) > 0)
    <div class="alert alert-danger">
     <ul>
     @foreach($errors->all() as $error)
      <li>{{ $error }}</li>
     @endforeach
     </ul>
    </div>
   @endif

Step 18. Write down the following code to display an error message within login form file:

@if ($message = Session::get('error'))
   <div class="alert alert-danger alert-block">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong>{{ $message }}</strong>
   </div>
   @endif

Step 19. Now, Create a function checklogin to within the MainController file.

function successlogin()
    {
     return view('successlogin');
    }

    

Step 20. Create a successlogin form file within resources/views

Step 21. Create a function logout to within the MainController file.

 function logout()
    {
     Auth::logout();
     return redirect('main');
    }

Step 22. Go to web.php file and we have to set route of all requests.

Route::get('/main', 'MainController@index');
Route::post('/main/checklogin', 'MainController@checklogin');
Route::get('main/successlogin', 'MainController@successlogin');
Route::get('main/logout', 'MainController@logout');

Step 23. Write down the following statement code within the login form.

 @if(isset(Auth::user()->email))
    <script>window.location="/main/successlogin";</script>
   @endif

Thanks