#Auth::Login or Auth::loginUsingId not working

7 messages · Page 1 of 1 (latest)

acoustic beacon
#

For brevity I will only include the code where I used loginUsingId

public function login(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required',
]);

$url = 'redacted url';
$user = $request->input('email');
$pass = $request->input('password');

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
    CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'username' => $user,
        'password' => urlencode($pass)
    ),
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false
));

$resp = curl_exec($curl);

if (curl_errno($curl)) {
    return back()->withErrors('login_error', "Curl error: " . curl_errno($curl));
}

$resp_data = json_decode($resp, true);
if ($resp_data['status'] == '1') {
    $employee = \App\Models\Employee::where('ldap', $resp_data['code_ldap'])->first();

    if ($employee === null) {
        Log::info('No corresponding Employee found for this ID');
        return back()->withErrors('login_error', "No corresponding Employee found for this ID");
    }

    // Use Laravel's built-in authentication system
    
    auth::loginUsingId($employee->id, true);
    @dd(auth::user());
    if (Auth::check()) {
        Log::info('User is logged in');
        Log::info('User object after login: ' . print_r(Auth::user(), true));
        return redirect()->route('dashboard')->with('status', 'Login successful');
    } else {
        Log::info('User is not logged in');
        return back()->withErrors('login_error', 'Authentication failed.');
    }
} else {
    return back()->withErrors('login_error', 'Invalid Login Credentials');
}

}

this gives me null, meaning that the user is not logged in. any tip

sterile folio
acoustic beacon
#

no its fine I found the thing

#

I had to create a guard for the employee model

sterile folio
#

Just use roles instead.

acoustic beacon
#

i am following this