Laravel 8: Recover Data from Logged In User

Asked

Viewed 173 times

-1

Hello,

i do not use a suggested default user table in Laravel, as I am creating a frontend in a database already exists.

The whole login process is working properly, with password user verification. But my problem is that when it calls the menu I cannot recover the data from the user who was logged in to the system. In the menu when I put dd(auth()->user()) it returns null.

I wanted to know what is missing in my code so I can recover user login data.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\UserRepository;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\Auth;
use Exception;
Use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    private $repository;
    private $validator;

    public function __construct(UserRepository $repository, UserValidator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

    public function index()
    {
        return view('user.dashboard');
    }

    public function auth(Request $request)
    {
        
        $data = [
            'U_NAME'    => $request->get('username'),
            'PASSWORD4' => $request->get('password')
        ];
        try
        {
            if(env('PASSWORD_HASH'))
            {
                Auth::attempt($data, false);
            }
            else
            {
                DB::enableQueryLog();
                $user = $this->repository->findwhere(['USER_CODE' => $request->get('username')])->first();
                if(!$user)
                {
                    return redirect()->route('user.login')->withErrors(['Usuário Inválido']);
                }
                if((trim($user->PASSWORD4)) != (trim($request->get('password'))))
                {
                    return redirect()->route('user.login')->withErrors(['Senha Incorreta']);
                }
                Auth::login($user);
            }
            return redirect()->route('user.dashboard');
        }
        catch (Exception $e)
        {
            return $e->getMessage();
        }
    }
}
  • How you set up the class of custom users, only with the Login part becomes complicated to know!

1 answer

0

I managed to solve by putting the information in Sesssion, below the new class with the change

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Repositories\UserRepository;
use App\Validators\UserValidator;
use Illuminate\Support\Facades\Auth;
use Exception;
Use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{
    private $repository;
    private $validator;

    public function __construct(UserRepository $repository, UserValidator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

    public function index()
    {
        return view('user.dashboard');
    }

    public function auth(Request $request)
    {
        $data = [
            'U_NAME'    => $request->get('username'),
            'PASSWORD4' => $request->get('password')
        ];
        try
        {
            if(env('PASSWORD_HASH'))
            {
                Auth::attempt($data, false);
            }
            else
            {
                DB::enableQueryLog();
                $user = $this->repository->findwhere(['USER_CODE' => $request->get('username')])->first();
                if(!$user)
                {
                    return redirect()->route('user.login')->withErrors(['Usuário Inválido']);
                }
                if((trim($user->PASSWORD4)) != (trim($request->get('password'))))
                {
                    return redirect()->route('user.login')->withErrors(['Senha Incorreta']);
                }
                Auth::login($user);
            }
                $request->session()->put('USER_CODE', $user->USER_CODE);
                $request->session()->put('U_NAME',    $user->U_NAME);
                $request->session()->put('SUPERUSER', $user->SUPERUSER);
                return redirect()->route('user.dashboard');
        }
        catch (Exception $e)
        {
            return $e->getMessage();
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.