Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5 Auth:true()始终返回false_Php_Laravel 5.2 - Fatal编程技术网

Php Laravel 5 Auth:true()始终返回false

Php Laravel 5 Auth:true()始终返回false,php,laravel-5.2,Php,Laravel 5.2,我正在尝试使用multi-auth进行自定义登录。同时,我正在尝试为管理员登录。当管理员登录时,登录函数会处理它(它也只是在没有登录函数的情况下刷新)Auth:trunt()似乎总是返回false,但是(我有不同的表名和字段)。除此之外,即使用户没有真正登录,我也可以通过更改url自由访问仪表板 授权控制器 /* |-------------------------------------------------------------------------- | Regist

我正在尝试使用multi-auth进行自定义登录。同时,我正在尝试为管理员登录。当管理员登录时,登录函数会处理它(它也只是在没有登录函数的情况下刷新)
Auth:trunt()
似乎总是返回false,但是(我有不同的表名和字段)。除此之外,即使用户没有真正登录,我也可以通过更改url自由访问仪表板

授权控制器

/*
    |--------------------------------------------------------------------------
    | 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' => 'AdminController@showDashboard'));

    });

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

        //Route for login
        Route::get('admin/login','AdminAuth\AuthController@showLoginForm');
        Route::post('admin/login','AdminAuth\AuthController@login');
        Route::get('admin/logout','AdminAuth\AuthController@logout');

        //Route for registration
        Route::get('admin/ims-register', 'AdminAuth\AuthController@showRegistrationForm');
        Route::post('admin/ims-register', 'AdminAuth\AuthController@register');

    }); 
重定向验证(来宾中间件)

我刚刚开始学习MVC框架并开始使用Laravel。谢谢你的帮助

注释

我的密码使用bcrypt()存储,列长为255

我已经尝试使用hash::check检查表中的哈希是否与我的输入匹配。这是真的。但当我这么做的时候:

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


试着根据这个问题的答案检查结果,尤其是#7。还是一样。

问题似乎出在这条线路上

'OPassword' => $password
我把它改成了

'password' => $password
它必须是密码而不是密码。然后在我的管理模型中,我指定了

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

检查这个答案。这对我很有用。@МааааааССаПаааааа107。
public function getAuthPassword()
{
    return $this->OPassword;
}