首页 > 解决方案 > Laravel 多表认证未登录

问题描述

在我的 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.php&的中间件中添加了额外的 2 个文件Redirectifnotagency.php

以下是代码

<?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);
        }
    }

这里有什么问题。请帮忙

标签: phplaravellaravel-5laravel-authentication

解决方案


推荐阅读