I am enhancing a system with a user login present (for staffs) and I want to create another login instance for clients. So I have set up the guards and coded the rest as follows
// config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'client' => [
'driver' => 'session',
'provider' => 'clients',
],
'client-api' => [
'driver' => 'token',
'provider' => 'clients',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Client::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
// app/Client.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Client extends Authenticatable
{
use Notifiable;
protected $guard = 'clients';
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
// routes/web.php
Route::prefix('/client')->name('client.')->namespace('Client')->group(function(){
Route::namespace('Auth')->group(function(){
//Login Routes
Route::get('/login','LoginController@showLoginForm')->name('login');
Route::post('/login','LoginController@login');
Route::post('/logout','LoginController@logout')->name('logout');
});
Route::get('/', 'HomeController@index')->name('home');
});
// app/Http/Controllers/Client/Auth/LoginController.php
namespace App\Http\Controllers\Client\Auth;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function showLoginForm() {
return view('auth.client.login');
}
public function login(Request $request) {
$this->validator($request);
if(Auth::guard('client')->attempt(['CustomerNo' => $request->username, 'password' => $request->password],$request->filled('remember'))) {
return redirect()
->intended(route('client.home'))
->with('status','You are Logged in as Admin!');
}
return $this->loginFailed();
}
private function validator(Request $request)
{
$rules = [
'username' => 'required|exists:clients,CustomerNo',
'password' => 'required',
];
$messages = [
'username.exists' => 'These credentials do not match our records.',
];
$request->validate($rules,$messages);
}
private function loginFailed(){
return redirect()
->back()
->withInput()
->with('error','Login failed, please try again!');
}
public function logout()
{
Auth::guard('client')->logout();
return redirect()
->route('client.login')
->with('status','You are logged out!');
}
}
// app/Http/Middleware/Authenticate.php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
protected function redirectTo($request)
{
if (Auth::guard('client')->check()) {
return redirect('/client');
} else {
return redirect('/home');
}
if (! $request->expectsJson()) {
return route('login');
}
}
}
// app/Http/Middleware/RedirectIfAuthenticated.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
if ($guard == "client" && Auth::guard($guard)->check()) {
return redirect(route('client.home'));
}
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
return $next($request);
}
}
// app/Http/Controllers/Client/HomeController.php
namespace App\Http\Controllers\Client;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth:client');
}
public function index()
{
return view('client.home');
}
}
I have tried to login after finishing all this but it seems to keep redirecting me back to staff's (default user login) login page and a quick check at the session dd
#session: Store {#195 ▼
#id: "wQTohtl4aNaSZvYZ9ru3oqltcSqs4VhWb4SV42t2"
#name: "portal_session"
#attributes: array:5 [▼
"_token" => "R6EyBlq0iNyfDh1V8Ogmta6U3uOhvJ0gcj7a2RMb"
"url" => array:1 [▼
"intended" => "https://mywebsite.com/home"
]
"_flash" => array:2 [▼
"old" => []
"new" => []
]
"_previous" => array:1 [▼
"url" => "https://mywebsite.com/client/login"
]
"login_client_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 1
]
#handler: FileSessionHandler {#194 ▼
#files: Filesystem {#95}
#path: "/home/mywebsite/storage/framework/sessions"
#minutes: 1440
}
#started: true
}
Is there any part where i did wrongly? Any advice is appreciated. Thanks in advance.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2u0zfEO
via IFTTT
Aucun commentaire:
Enregistrer un commentaire