Bonjour;
Je souhaiterais faire un multi authentification pour séparer ma table recipients et admins sur des tables différentes.
J'ai suivi ce tutoriel: https://hdtuto.com/article/laravel-57-multiple-authentication-example
J'arrive à me connecter en admin ou en recipient mais une fois que je change de page la session n'est pas gardé.
Une idée d'où cela peut venir ?
web.php
Route::redirect('/', '/home');
Route::get('/login', 'AuthController@formLogin');
Route::post('/login', 'AuthController@processLogin');
Route::group([
'middleware' => 'App\Http\Middleware\Authenticate'
], function () {
Route::view('/home', 'home');
});
auth.php
<?php
return [
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'recipients',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
'providers' => [
'recipients' => [
'driver' => 'eloquent',
'model' => App\Recipient::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => \App\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
AuthController.php (Controller)
<?php
namespace App\Http\Controllers;
use App\Admin;
use App\Recipient;
use Illuminate\Http\Request;
class AuthController extends Controller
{
private $viewPath = 'Account/';
private function errorLogin(int $code = 0)
{
switch ($code) {
case 1:
$error = 'Wrong email or password';
break;
case 2:
$error = 'Your blocked';
break;
case 3:
$error = 'Your company is blocked';
break;
case 4:
$error = 'Your email is not validated';
break;
case 5:
$error = 'Not find in our second database';
break;
default:
$error = 'Indisponible connection';
}
return back()->withInput()->withErrors([
'email' => $error
]);
}
private function auth(string $email, string $password, string $uType = 'web')
{
$auth = auth($uType)->attempt([
'email' => $email,
'password' => $password
]);
return $auth;
}
private function checkAuth(string $email, string $password): bool
{
if (!$this->auth($email, $password)) {
if (!$this->auth($email, $password, 'admin')) {
return false;
}
}
auth()->logout();
return true;
}
public function formLogin() {
if (auth()->guest()) {
return view($this->viewPath . 'login');
}
return redirect('/home');
}
public function processLogin(Request $request) {
$request->validate([
'email' => ['required', 'email'],
'password' => ['required']
]);
if (!$this->checkAuth($request->email, $request->password)) {
return $this->errorLogin(1);
}
$uType = 'web';
$user = Recipient::where('email', $request->email)->first();
if (!$user) {
$user = Admin::where('email', $request->email)->firstOrFail();
$uType = 'admin';
}
if ($user->blocked) {
return $this->errorLogin(2);
}
$this->auth($request->email, $request->password, $uType);
dd(auth('admin')); //dd(auth('recipient')); --> Ici c'est ok je suis bien log
return redirect('/home');
}
}
Authenticate.php (Middleware)
<?php
namespace App\Http\Middleware;
use Closure;
class Authenticate
{
public function handle($request, Closure $next)
{
dd(auth('admin')); //dd(auth('recipient')); --> Ici plus rien
return redirect('/login');
return $next($request);
}
}
Admin (Model)
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
}
Recipient (Model)
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Recipient extends Authenticatable
{
}
Apres login:
http://www.zupimages.net/viewer.php?id=19/18/jx1m.png
Une fois sur le middleware:
http://zupimages.net/viewer.php?id=19/18/5jdp.png