0

I made a multi auth system in laravel 6 one for admins and the other for visitors

So I made a second guard with the name visitor

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'visitor' => [
        'driver' => 'session',
        'provider' => 'visitors',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'visitors' => [
        'driver' => 'eloquent',
        'model' => App\Visitor::class,
    ],

],

the LoginController.php is:

<?php

namespace App\Http\Controllers\web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class LoginController extends Controller
{


protected $loginRoute;

public function __construct()
{
    $this->middleware('guest:visitor');
}

public function showForm ()
{
    return view('web.login');
}

public function login(Request $request)
{
    $this->validate($request, [
        'phone' => 'nullable',
        'password' => 'required|min:8'
    ]);

    $credentials = ['phone' => $request->phone, 'password' => $request->password ];
    if(Auth::guard('visitor')->attempt($credentials, $request->remember) ){
        return redirect()->intended(route('visitor.dashboard'));
    }

    return redirect()->back()->withInput($request->only('phone', 'remember'));
}

}

And I hash the passwords with bcrypt function

the problem is that whene I try to login to the first account it works and redirect me to home page with all account details, but the rest of accounts are not working, it keeps redirect me to login page like as I'm not logged in

And that's the Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;

class Handler extends ExceptionHandler
{
/**
 * A list of the exception types that are not reported.
 *
 * @var array
 */
protected $dontReport = [
    //
];

/**
 * A list of the inputs that are never flashed for validation exceptions.
 *
 * @var array
 */
protected $dontFlash = [
    'password',
    'password_confirmation',
];

/**
 * Report or log an exception.
 *
 * @param  \Exception  $exception
 * @return void
 *
 * @throws \Exception
 */
public function report(Exception $exception)
{
    parent::report($exception);
}

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @throws \Exception
 */
public function render($request, Exception $exception)
{
    return parent::render($request, $exception);
}


/**
 * @param \Illuminate\Http\Request $request
 * @param AuthenticationException $exception
 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    $guard = Arr::get($exception->guards(), 0);

    switch ($guard) {
        case 'visitor':
            $login='visitor.login';
            break;

        default:
            $login='admin.login';
            break;
    }

    return redirect()->guest(route($login));
}
}

And the RedirectIfAuthenticated.php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    switch ($guard) {

        case 'visitor':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('visitor.dashboard');
            }

        default:
            if (Auth::guard($guard)->check()) {
                return redirect('/admin/classrooms');
            }
            break;
    }

    return $next($request);
}
}

the web guard (set for admins) works fine for all accounts, the problem is in visitor guard

IBMdig
  • 163
  • 2
  • 12

1 Answers1

0
  • create new VisitorLoginController not LoginController,
  • LoginController for default user
dhamkith
  • 121
  • 3
  • no that controller is inside another folder i'm using a different namespace `namespace App\Http\Controllers\web;` – IBMdig Mar 28 '20 at 00:07