Tuesday, May 1, 2018

How to Create Authentication Login in Laravel 5

In this tutorial, we are going to cover the authentication system in the Laravel 5 framework. Laravel 5 provides a very solid authentication system in the core that makes the implementation of basic authentication. You may also like How to integrate Paypal payment gateway using Laravel 5 and Shopping Cart using Laravel 5.

Steps For How to Create Authentication Login in Laravel 5

Step 1. Create route
We are required three route. one is view login page and second one is submit form using post method and last one is 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'); }

No comments:

Post a Comment