Use as like below because your not calling auth classes.
print_r(\Auth::user());
else
print_r(\Auth::user()->id);
I am facing an issue after I upgraded Laravel 5.2 to 5.6. I am using Valet on mac and I found that after I Auth::login() I could do Auth::user() and fetch the details on the same route where I had called Auth::login. The issue arises when I go to a different route and try to do Auth::user() nothing comes in return.
I also noticed that new sessions are getting created when I go to a new route to do Auth::user().
My kernel looks like this:-
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
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,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'adminmiddleware'=> \App\Http\Middleware\Adminmiddleware::class,
'clientmiddleware'=> \App\Http\Middleware\Clientmiddleware::class,
'adminpermissionmiddleware' => \App\Http\Middleware\AdminPermissionMiddleware::class,
'checklang'=> \App\Http\Middleware\Checklang::class,
// 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'editCategoryUrl'=> \App\Http\Middleware\EditCategoryUrl::class,
];
}
My route service provide
<?php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
// public function boot(Router $router)
// {
// //
//
// parent::boot($router);
// }
/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function map(Router $router)
{
$this->mapWebRoutes($router);
$this->mapApiRoutes($router);
$this->mapAdminRoutes($router);
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, // 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
}
/**
* added a seperate route for api routes
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapApiRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'api',
], function ($router) {
require app_path('Http/api-routes.php');
});
}
/**
* added a seperate route for api routes
* @param \Illuminate\Routing\Router $router
* @return void
*/
protected function mapAdminRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/admin-routes.php');
});
}
}
My routes where I am checking these
Route::group(['middleware' => 'web'], function()
{
// I load the view form
Route::get('test-view', function(){
return view('admin.loginTest');
});
//I try to login the user here.
Route::post('login-user', function(Request $request){
$email = $request->email;
$password = md5($request->password);
$admin = App\Admin::where([ ['email', $email], ['password', $password] ])->first();
if (!is_null($admin)) {
Auth::login($admin);
return redirect('/check-auth');
}
});
// I check the user logged in or not here
Route::get('check-auth', function(){
echo "<pre>";
print_r(Auth::user());
die;
});
});
My config/auth
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'admin',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| Here you may set the options for resetting passwords including the view
| that is your password reset e-mail. You may also set the name of the
| table that maintains all of the reset tokens for your application.
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
I tried to look out and found this link https://github.com/laravel/framework/issues/13000 which I suspect could be the issue but I have tried all of these and still face the issue.
Please assist if anyone has ever faced this issue. Thank you.
Please or to participate in this conversation.