Laravel Classroom image

osdyui
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5|Last
Lessons:-Authentication

14.3 Handling Login
Note that before we can login, we will need to setup a session driver. This is because Laravel
logins are stored in the session. If you head over to application/config/session.php it will
list all your options, go ahead and choose a suitable one.
Great! Let’s handle that login attempt and go to work on that post route. First let’s get the input
data that has been posted from the form.
1 <?php
2
3 Route::post('login', function() {
4
5 // get POST data
6 $userdata = array(
7 'username' => Input::get('username'),
8 'password' => Input::get('password')
9 );
10
11 });
Sweet! We have post data, lets put it to use. We will use the Auth::attempt() method to check
if the username and password can be used to login. The great thing about this method is on
success it will automatically create our ‘logged-in’ session for us! Mucho conveniento! (I never
took Spanish, sorry.).
1 <?php
2
3 Route::post('login', function() {
4
5 // get POST data
6 $userdata = array(
7 'username' => Input::get('username'),
8 'password' => Input::get('password')
9 );10
11 if ( Auth::attempt($userdata) )
12 {
13 // we are now logged in, go to home
14 return Redirect::to('home');
15 }
16 else
17 {
18 // auth failure! lets go back to the login
19 return Redirect::to('login')
20 ->with('login_errors', true);
21 // pass any error notification you want
22 // i like to do it this way :)
23 }
24
25 });
If our authentication is successful, the login session is created and we are redirected to the home
route. Perfect, but before we go any further let’s create a logout route so that we can logout to
perform future testing.
1 <?php
Route::get(‘logout’, function() {
1 Auth::logout();
2 return Redirect::to('login');
3 });
Here we use the Auth::logout() method to destroy the login session, and head back to the login
page. We are now logged out.
Right, now that we have our login working perfectly let’s create our super secret home page, add
a logout link, and welcome our currently logged in user.
1 <?php
2
3 //---- THE ROUTE ----
4
5 Route::get('home', function() {
6 return View::make('home');
7 });
8
9 //---- THE VIEW (home.blade.php) ----
1011 <div class="header">
12 Welcome back, {{ Auth::user()->username }}!<br />
13 {{ HTML::link('logout', 'Logout') }}
14 </div>
15
16 <div class="content">
17 <h1>Squirrel Info</h1>
18 <p>This is our super red squirrel information page.</p>
19 <p>Be careful, the grey squirrels are watching.</p>
20 </div>
Now you can test our login loop.. Login, be greeted with the welcome page, logout. Repeat. Do
this at least 800 times to set your mind to rest. Don’t worry, this is normal behaviour, besides..
you get paid by the hour right PHP Guru?
You will notice that we use Auth::user() in the above example, this will return a Fluent database
result object representing the current logged in user. Very handy for finding its id, or echoing
out welcome information.

 

 

 
 
 

osdyui

Skills    Laravel

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)