2017-02-19 54 views
0

私はマルチ認証でカスタムログインをしようとしています。その間、私はadminのログインをしようとしています。管理者がログインすると、ログイン機能がそれを処理します(ログイン機能なしで更新されるだけです)。しかし、Auth:attempt()は常にfalseを返すようです(別のテーブル名とフィールドがあります)。それはさておき、私は自由にユーザーが実際にログインしていない場合でも、単にURLを変更することで、ダッシュボードにアクセスすることができます。Laravel 5 Auth:attempt()は常にfalseを返します

AuthController

/* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    /** 
    * Where to redirect users after login/registration. 
    * 
    * @var string 
    */ 
    protected $redirectTo = 'admin/dashboard'; 

    /** 
    * Where to redirect users after logout. 
    * 
    * @var string 
    */ 
    protected $redirectAfterLogout = 'admin/login'; 

    /** 
    * Guard for admin 
    * 
    * 
    */ 
    protected $guard = 'admin'; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware($this->guestMiddleware(), ['except' => 'logout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 

    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'OUsername' => 'required|max:255|unique:users', 
      'OPassword' => 'required|min:6|confirmed', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return Admin::create([ 
      'OUsername' => $data['OUsername'], 
      'OPassword' => bcrypt($data['OPassword']), 
     ]); 
    } 

    /** 
    * Show login form. 
    * 
    * 
    * 
    */ 

    public function showLoginForm() 
    { 
     if (view()->exists('auth.authenticate')) { 
      return view('auth.authenticate'); 
     } 

     return view('pages.admin.login'); 
    } 

    /** 
    * Show registration form. 
    * 
    * 
    * 
    */ 

    public function showRegistrationForm() 
    { 
     return view('pages.admin.register'); 
    } 


    public function login(Request $request) 
    { 
     //Get inputs 
     $username = $request->input('username'); 
     $password = $request->input('password'); 

     //Redirect accordingly  
     if (Auth::guard('admin')->attempt(array('OUsername' => $username, 'OPassword' => $password))) 
     { 
      return redirect()->intended('admin/dashboard'); 
     } 

     else 
     { 
      //when echoing something here it is always displayed thus admin login is just refreshed. 
      return redirect('admin/login')->withInput()->with('message', 'Login Failed'); 
     } 
    } 

管理プロバイダモデル

/** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'account_officer_t'; 


    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'OUsername', 'OPassword', 
    ]; 

    public $timestamps = false; 

    /** 
    * Set primary key 
    * 
    * @var int 
    */ 
    protected $primaryKey = 'AccountOfficerID'; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'OPassword', 'remember_token', 
    ]; 

    public function getAuthPassword() 
    { 
     return $this->OPassword; 
    } 

ルート

/* 
    |-------------------------------------------------------------------------- 
    | Application Routes 
    |-------------------------------------------------------------------------- 
    | 
    | Here is where you can register all of the routes for an application. 
    | It's a breeze. Simply tell Laravel the URIs it should respond to 
    | and give it the controller to call when that URI is requested. 
    | 
    */ 

    Route::group(['namespace' => 'Admin', 'middleware' => 'guest'], function(){ 
//This uses the guest middleware with the class name RedirectIfAuthenticated 
     Route::auth(); 

     //Route for admin dashboard view 
     Route::get('admin/dashboard', array('as' => 'dashboard', 'uses' => '[email protected]')); 

    }); 

    Route::group(['middleware' => ['web']], function() { 

     //Route for login 
     Route::get('admin/login','AdminAuth\[email protected]'); 
     Route::post('admin/login','AdminAuth\[email protected]'); 
     Route::get('admin/logout','AdminAuth\[email protected]'); 

     //Route for registration 
     Route::get('admin/ims-register', 'AdminAuth\[email protected]'); 
     Route::post('admin/ims-register', 'AdminAuth\[email protected]'); 

    }); 

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) 
    { 
     if (Auth::guard('admin')->check()) {   
      return redirect('admin/dashboard'); 
     } 

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

     return $next($request); 
    } 

私はMVCフレームワークの学習を始め、Laravelを使い始めました。お手伝いありがとう。

ノートが

私のパスワードは私がテーブルからハッシュハッシュを使用して、私の入力と一致するかどうかをチェックしようとしている255

の列の長さとbcryptのを()を使用して保存されます::確認してください。真を返します。しかし、私がこれをするとき:

dd(Auth::guard('admin')->attempt(array('OUsername' => $username, 'OPassword' => $password))); 

それは偽です。

回答は、question(特に#7)の回答に基づいてチェックしてみました。

+0

を指定された私の管理モデル[laravel 5.2でマルチ認証を使用する方法](中http://stackoverflow.com/questions/34490600/how-to-use-multi-auth-in-laravel -5-2)この答えを確認してください。それは私のために働く。 –

+0

@МаксимСтепановダッシュボードで問題を解決しますが、ログインでは解決しません。 –

答えて

0

問題は、このライン

'OPassword' => $password 

であるように思わ私はそれがパスワードではありませんOPasswordにする必要があり

'password' => $password 

にそれを変更しました。そして、私は

public function getAuthPassword() 
{ 
    return $this->OPassword; 
} 
関連する問題