29.07.2016 Views

laravel-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Authentication 146<br />

However, you may use middleware to verify that the user is authenticated before allowing the user<br />

access to certain routes / controllers. To learn more about this, check out the documentation on<br />

protecting routes.<br />

Protecting Routes<br />

Route middleware can be used to allow only authenticated users to access a given route. Laravel<br />

ships with the auth middleware, which is defined in app\Http\Middleware\Authenticate.php. All<br />

you need to do is attach the middleware to a route definition:<br />

.<br />

1 // Using A Route Closure...<br />

2<br />

3 Route::get('profile', ['middleware' => 'auth', function() {<br />

4 // Only authenticated users may enter...<br />

5 }]);<br />

6<br />

7 // Using A Controller...<br />

8<br />

9 Route::get('profile', [<br />

10 'middleware' => 'auth',<br />

11 'uses' => 'ProfileController@show'<br />

12 ]);<br />

Of course, if you are using controller classes, you may call the middleware method from the<br />

controller’s constructor instead of attaching it in the route definition directly:<br />

.<br />

1 public function __construct()<br />

2 {<br />

3 $this->middleware('auth');<br />

4 }<br />

Authentication Throttling<br />

If you are using Laravel’s built-in AuthController class, the Illuminate\Foundation\Auth\ThrottlesLogins<br />

trait may be used to throttle login attempts to your application. By default, the user will not be able<br />

to login for one minute if they fail to provide the correct credentials after several attempts. The<br />

throttling is unique to the user’s username / e-mail address and their IP address:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!