Laravel多表身份验证未登录

时间:2020-04-21 06:20:18

标签: php laravel laravel-5 laravel-authentication

在我的laravel项目中,我正在为代理商实施另一个登录,我想从代理商表中获取。代理商表具有emailpassword字段。但是当我尝试登录时却无法登录并重定向到同一页面并出现验证错误。但是我提供了准确度表中的准确电子邮件和密码

以下是我在模型中的代码

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Agencie extends Authenticatable
{
    use Notifiable;
    use Sortable;

    protected $guard = 'agencie';
    protected $table = 'agencies';
    protected $primaryKey = 'agency_id';    

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'agency_id', 'agency_name','agency_user_name' ,'agency_password', 'agency_city', 'agency_state', 'agency_zip', 'agency_email','status','created_at','updated_at'
    ];

}

下面我已经添加到config / auth.php

'guards' => [
        'agencie' => [
            'driver' => 'session',
            'provider' => 'agencies',
        ],   
    ],
'providers' => [
        'agencies' => [
            'driver' => 'eloquent',
            'model' => App\Agencie::class,
        ],
    ],

以下是我在LoginController中的代码

<?php

namespace App\Http\Controllers\Agency\AgencyAuth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Hesto\MultiAuth\Traits\LogsoutGuard;
use JsValidator;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers, LogsoutGuard {
        LogsoutGuard::logout insteadof AuthenticatesUsers;
    }


    protected $validationRules = [
                                        'email' => 'required|email',
                                        'password' => 'required'
                                    ];

    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    // public $redirectTo = '/user/home';
    public $redirectTo = '/user/dashboard-graph';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

        $this->middleware('guest:agencie', ['except' => 'logout']);

       // $this->middleware('guest:agency')->except('logout');
    }

    public function username()
    {
    return 'agency_email';
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        $validator = JsValidator::make($this->validationRules,[],[],'#loginform');
        return view('agency.auth.login')->with('validator', $validator);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('agencie');
    }
    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */   
}

我已将以下代码添加到app / Expecations / Handler.php

 protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        $middleware = request()->route()->gatherMiddleware();
        $guard = config('auth.defaults.guard');
        foreach($middleware as $m) {
            if(preg_match("/auth:/",$m)) {
                list($mid, $guard) = explode(":",$m);
            }
        }

        switch($guard) {
            case 'agencie':
                $login = 'agency/login';
                break;
            default:
                $login = 'user/login';
                break;
        }

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

我还在名为RedirectifAgency.phpRedirectifnotagency.php的中间件中添加了另外2个文件

以下是代码

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

    class RedirectIfAgency
    {
        /**
         * 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 = 'agencie')
        {
            if (Auth::guard($guard)->check()) {
                return redirect('agency/home');
            }

            return $next($request);
        }
    }

这是什么问题。请帮助

0 个答案:

没有答案