Laravel 5 Auth:attempt()始终返回false [英] Laravel 5 Auth:attempt() always returns false

查看:88
本文介绍了Laravel 5 Auth:attempt()始终返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多身份验证进行自定义登录.同时,我正在尝试为admin登录.管理员登录时,登录功能会对其进行处理(它也会在没有登录功能的情况下进行刷新),但是Auth:attempt()似乎总是返回false,但是(我有不同的表名和字段).除此之外,即使用户没有真正登录,我也可以通过更改URL来自由访问仪表板.

I am trying to make a custom login with multi auth. For the meantime, I am trying to do the login for admin. When an admin logs in, the login function handles it (it also just refreshes without the login function) Auth:attempt() seems to be always returning false, however (I have a different table name and fields). Aside from that, I can freely access the dashboard by just changing the url even if the user is not really logged in.

AuthController

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' => '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');

    }); 

RedirectIfAuthenticated(访客中间件)

RedirectIfAuthenticated (guest middleware)

/**
     * 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.谢谢您的帮助.

I have just started learning the MVC framework and started using Laravel. Thank you for the help.

注释

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

My passwords are stored using bcrypt() with column length of 255

我尝试使用Hash :: check检查表中的哈希是否与我的输入匹配.它返回true.但是当我这样做时:

I have tried checking if the hash from the table matches my input using Hash::check. It returns true. But when I do this:

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

这是错误的.

尝试根据此问题的答案检查结果尤其是#7.还是一样.

Tried checking the results based on the answer from this question especially # 7. Still the same.

推荐答案

问题似乎出在这行

'OPassword' => $password

我将其更改为

'password' => $password

它必须是密码而不是OPassword.然后在我的管理员模型中指定

It has to be password not OPassword. And then in my Admin model I specified

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

这篇关于Laravel 5 Auth:attempt()始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆