Mar 15, 2024
0
Level 1
Auth::user() empty, using custom user provider
Hello, Using Laravel 10 i'm setting up a custom user provider, because i'm using an SSO service (CAS), the user provider i wrote just fetch the currently logged in user (via subfission\cas module) and instantiate and return the corresponding model so it can be accessed by Auth::user().
the steps i've taken:
- Create a model which implements the Illuminate\Contracts\Auth\Authenticatable interface (i do keep some user related infos in local DB but Authentication doesn't take place here):
use Illuminate\Foundation\Auth\User as BaseUser;
use Illuminate\Contracts\Auth\Authenticatable;
class User extends BaseUser implements Authenticatable
{
use HasFactory;
protected $table = 'user';
public function getAuthIdentifierName() {
return 'uid';
}
public function getAuthIdentifier() {
return app('cas')->user();
}
public function getAuthPassword() {
return null;
}
public function getRememberToken() {
return null;
}
public function setRememberToken($value) {
}
public function getRememberTokenName() {
return null;
}
- Create a custom user provider which returns instances of the above model:
namespace App\Providers;
// use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Services\UserService;
use App\Models\User;
class CasUserProvider implements UserProvider
{
public function __construct(UserService $user_service) {
$this->user_service = $user_service;
}
public function retrieveById($identifier): ?Authenticatable {
$uid = app('cas')->user();
$user = $user_service->findByUid($uid);
return $user;
}
public function retrieveByToken($identifier, $token): ?Authenticatable {
$uid = app('cas')->user();
$user = $user_service->findByUid($uid);
return $user;
}
public function updateRememberToken(Authenticatable $user, $token) {
}
public function retrieveByCredentials(array $credentials): ?Authenticatable {
$uid = app('cas')->user();
$user = $user_service->findByUid($uid);
return $user;
}
public function validateCredentials(Authenticatable $user, array $credentials) {
return true;
}
}
- Register it in AuthServiceProvider:
public function boot(): void
{
Auth::provider('cas_user', function (Application $app, array $config) {
return new CasUserProvider(new UserService());
});
}
- Adding the provider in conf\auth.php and set the default 'web' guard to use it:
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
// conf par défaut
'web' => [
'driver' => 'session',
'provider' => 'cas_users',
// 'credentials' => [
// 'email' => 'login',
// 'password' => 'password'
// ]
],
],
'providers' => [
// conf par défaut
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'cas_users' => [
'driver' => 'cas_user',
],
],
The Auth is done via a middleware and this part is working, also i don't get any errors, but calls to Auth::user() in controllers, or auth()->user() in blade templates always yield empty result. Any help would be greatly appreciated
Please or to participate in this conversation.