AdRock
4 months ago
59
16
Laravel

Auth::user() returns null using custom login

Posted 4 months ago by AdRock

I am having a problem getting a logged in user's details in other controllers. If i do dd(Auth::user()) in any controller it returns null

Here is my LoginController

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
     public function __construct()
     {
         $this->middleware('guest')->except('logout');
     }

     public function login(Request $request)
     {
        $xys = '1234'
        $user = \User::whereRaw('password = AES_ENCRYPT("' . $request->input('password') . '", "' . $xyz . '")')
            ->where('username', $username)
            ->where('portalAccess', 1)
            ->where('isActive', 1)
            ->first();
        
         // Set Auth Details
         \Auth::login($user);

         dd(\Auth::user()->portalPermissions);

         // Redirect home page
         return redirect()->route('home');
     }
}

And my routes

Route::group(['prefix' => 'reports', 'as' => 'reports', 'middleware' => ['company']], function() {
    Route::get('/', ['as' => 'index', 'uses' => '[email protected]']);

I can get the authed user in my LoginContoller no problem and I can see their permissions but not in any other controllers. Do I need to do something different in my routes. It's been a while since I last did this?

Please sign in or create an account to participate in this conversation.