Laravel: Creating a Simple Login Form

Creating a Simple Login Form Using Laravel
Laravel has been one of the widely chosen web application framework used for creating web applications. As a matter of fact, the popularity of this open source framework has been increasing with time. This PHP based framework can be used to result oriented and maintainable code.

In the previous blog we have learnt how to create user registration form using Laravel and save details to database. Now let us see how to create a simple login form using it.

Create a page login.blade.php in ‘app/views’ directory and place this code:

 
<h1>Sign In</h1>

@if ($errors->any ())

<ul>

{{implode ('', $errors->all ('<li>: message</li>'))}}

</ul>

@endif

{{Form::open (array ('url' => 'logincheck'))}}

<p> {{Form::text ('username', ‘‘, array ('placeholder'=>'Username','maxlength'=>30))}} </p>

<p> {{Form::password ('password', array('placeholder'=>'Password','maxlength'=>30)) }} </p>

<p> {{Form::submit ('Submit')}} </p>

{{Form::close ()}}

This will create a simple login form with action = ‘logincheck’ and method=’POST’, this form asks only username and password.

We have placed a message section above the form to notify upon error. This section will check if there is any item in ‘errors’ array (‘errors’ array is set from login action using ‘withErrors’ given below).

Now add a route to access this page

Open ‘ app/routes.php’ and add

 
Route::get ('login',function(){

Return View::make ('login');

});

Now, we have to add a new route for ‘logincheck’ in our ‘ app/routes.php’, so add these lines of code

Route::post('logincheck', function(){

// username and password validation rule

$rules = array (

'username' => 'required|max:25',

'password' => 'required|max:25',

);

$v = Validator::make (Input::all (), $rules);

if ($v->fails()) {

// username or password missing

// validation fails

// used to retain input values

Input::flash ();

// return to login page with errors

return Redirect::to('login')

->withInput()

->withErrors ($v->messages ());

} else {

$userdata = array (

'username' => Input::get('username'),

'password' => Input::get('password')

);

If (Auth::attempt ($userdata)) {

// authentication success, enters home page

return Redirect::to('home');

} else {

// authentication fail, back to login page with errors

return Redirect::to('login')

->withErrors('Incorrect login details');

}

}

});

After login success page redirects to the url ‘/home’, so we have to create a view for that.

Create a page home.php in ‘app/views’ directory and place this code:

 
<h1>Home Page</h1>

<h5>Home page contents</h5>

Now open ' app/routes.php' and add a route to this file

Route::get ('home',function(){

return View::make('home');

});

In this example, we used Auth::attempt method (laravel built-in function) to authenticate user credentials.

NOTE: you don’t need to hash password while using Auth::attempt method, because by default ‘attempt’ method will hash the password

You can use Auth::check () method to check whether user is logged or not, Auth::check () will return TRUE if logged and FALSE if not logged.

Do you have a set of routes that need to be accessed only by logged users? You can check whether the user has logged before going to a route using ‘auth’, i.e.:

 
Route::group (array ('before' => 'auth'), function () {

// list routes that needs authentication

});

In our example we can place our ‘home’ route inside this, now it might look like:

// check if logged for a set of routes

 
Route::group (array ('before' => 'auth'), function () {

Route::get ('home',function(){

return View::make('home');

});

});

Now, when user tries to access home page, Laravel, checks whether he is logged. If yes, goes to the desired route.

If checking fails redirects to the URL defined in the Redirect::guest () given in ‘Authentication Filters’ section in ‘app/filters.php’.

One more function need to be added. i.e., if logged, user needs to enter home page upon accessing login page.

For this we need to add a simple authentication checking in login page route. New login route look like this:

Route::get (‘/login’,function(){

// if logged redirect to home page

 
If (Auth::check ()) {

return Redirect::to('/home');

}

return View::make('login');

});

At last let us see how to logout from the current session.

Create a link for logout

 
{{link_to('/logout','Logout')}} and place it in the home page (header of logged pages).

this is same as <a href="/logout">Logout</a>

Then add route for logout action, it is very simple as given below:

 
Route::get ('logout',function(){

Auth::logout ();

return Redirect::to('login');

});

This clears logged user session, and then returns to login page.

This is a very simple blog just to show how the Laravel built in authentication functions (Auth::attempt, Auth::check, Auth::logout) work. Hope you all got an idea about creating a login form using Laravel.

Know More About This Topic from our Techies

Latest Post