Laravel 5.2 Auth not Working
Laravel 5.2 introduces the middleware groups concept: you can specify that one or more middleware belongs to a group, and you can apply a middleware group to one or more routes
By default Laravel 5.2 defines a group named web
, used to group the middleware handling session and other http utilities:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
So, if you want session handling, you should use this middleware group for all the routes in which you want to use authentication:
Route::group( [ 'middleware' => ['web'] ], function ()
{
//this route will use the middleware of the 'web' group, so session and auth will work here
Route::get('/', function () {
dd( Auth::user() );
});
});
UPDATE FOR LARAVEL VERSION >= 5.2.27
As of Laravel 5.2.27
version, all the routes defined in routes.php
are using by default the web
middleware group. That is achieved in app/Providers/RouteServiceProvider.php
:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}
So you don't need anymore to add manually the web
middleware group to your routes.
Anyhow, if you want to use the default authentication for a route, you still need bind the auth
middleware to the route
Laravel 5.2 Auth login works, Auth::check fails
So I ended up figuring out the solution, I just wish this wasn't down-voted. But oh well, on my model I never indicated a primary key and as such when retrieving the user it was not possible.
To fix for me it simply required this line
protected $primaryKey = 'client_idx_NW';
Laravel 5.2 /login & /register not working
You were logged in & cookies were keeping track of your authentication status.
Thus you are redirected to home page
when you visit
/login
/register
In Laravel Auth, Logged-In users can't view login
page or register
page. You have to either logout OR delete the cookies (not a recommended way) to view these pages again.
Related Topics
Php: Split Multibyte String (Word) into Separate Characters
How to Determine a File's True Extension/Type Programmatically
Getting a PHP Pdo Connection from a MySQL_Connect()
Php: Strtotime Is Returning False for a Future Date
Pdo Returning Incorrect, But Duplicate, Data. Key's Not in Database
Mysqli Prepared Statement Column with Variable
Tactics for Using PHP in a High-Load Site
How to Convert Array Values to Lowercase in PHP
How to Properly Use Bearer Tokens
Adding a Namespace When Using Simplexmlelement
Error 1148 MySQL the Used Command Is Not Allowed with This MySQL Version
Interface Implementation: Declaration Must Be Compatible
PHP Run Once and Insert Twice in MySQL Database
File_Get_HTML Displays Fatal Error Call to Undefined Function
Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript