My stack:
- Laravel 8
- PHP 7.4
- DoctrineORM
- LDAP Auth
I'm trying to do a manual authentication on Laravel (Laravel 8).
But, when I try to save a $user variable in Auth, it is not saved.
Login Controller:
public function auth(LoginRequest $request)
{
$auth = Adldap::auth()->attempt($request->user.env('LDAP_USER_SUFIX'), $request->password, true);
if($auth){
$userData = Ldap::ldapUserAttributes($request->user);
$user = new User($userData['name'], $userData['mail']);
Auth::login($user);
return redirect()->route('dashboard');
}
return redirect()->back()->withErrors('Usuário e/ou senha incorretos');
}
The User class implements the Authenticatable, but it isn't a database entity.
These are my routes (web.php):
Route::get ('/login', [Login::class, 'index']) ->name('login');
Route::post('/login', [Login::class, 'auth']) ->name('auth');
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [Dashboard::class, 'index'])->name('dashboard');
});
If I remove the authentication middleware and print what it receives, NULL is printed.
But, if I print Auth content right after Auth::login($user); at Login Controller, the User data is printed on screen.
This is my User implementation:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable;
class User implements Authenticatable
{
private $cpf;
private $username;
private $email;
private $senha;
private $remember_token;
public function __construct(string $cpf, string $username, string $email)
{
$this->cpf = $cpf;
$this->username = $username;
$this->email = $email;
}
public function getUsername()
{
return $this->username;
}
public function getEmail()
{
return $this->email;
}
public function getCpf()
{
return $this->cpf;
}
public function getAuthIdentifierName()
{
return $this->username;
}
public function getAuthIdentifier()
{
return $this->cpf;
}
public function getAuthPassword()
{
return $this->senha;
}
public function getRememberToken()
{
return $this->remember_token;
}
public function setRememberToken($value)
{
$this->remember_token = $value;
}
public function getRememberTokenName()
{
return $this->remember_token;
}
}
PS.: Session::set('test', 'test'); and Session::get('test') work for me in diferents routes.
$middlewareGroups in the Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
My Session is set to save in file