使用正确的用户名/密码组合时,Laravel认证无法工作


Laravel auth not working using correct username/password combination

由于某种原因,我无法使用正确的用户名/密码组合登录。当登录信息不正确时,我使用flash消息来显示。

我试着创建多个帐户,以确保我实际上没有输入错误的登录凭据,但经过大约一个小时的摆弄,它仍然不起作用。

任何帮助都将非常感激!谢谢你!

这是我得到的。

UsersController.php (postCreate) -函数创建我的用户帐户(工作完全正常)

public function postCreate() {
        $rules = array(
        'username'=>'required|unique:users,username|alpha_dash|min:4',
        'password'=>'required|min:8|confirmed',
        'password_confirmation'=>'required|min:8',
        'email'=>'required|email|unique:users,email'
        );
        $input = Input::only(
            'username',
            'password',
            'password_confirmation',
            'email'
        );
        $validator = Validator::make($input, $rules);
        if($validator->fails())
        {
            return Redirect::to('register')->withErrors($validator)->withInput();
        }
        //$confirmation_code = str_random(30);
        User::create(array(
            'username'=>Input::get('username'),
            'password'=>Hash::make(Input::get('password')),
            'email'=>Input::get('email')
            //'comfirmation_code' => $confirmation_code
        ));
        // Mail::send('email.verify', function($message) {
        //  $message->to(Input::get('email'), Input::get('username'))
        //      ->subject('Verify your email address');
        // });
        // Flash::message('Thanks for signing up! Please check your email.');
        return Redirect::to('login')->with('message', 'Thanks for signing up! Please check your email.');
    }

UsersController.php (postLogin) -登录帐户的函数

public function postLogin() {
        $user = array(
            'email'=>Input::get('email'),
            'password'=>Input::get('password')
        );
        if (Auth::attempt($user)){
            return Redirect::intended('account')->with('message', 'Welcome back!');
        } else {
            return Redirect::to('login')
                ->with('message', 'Your username/password was incorrect')
                ->withInput();
        }
    }

Routes.php

Route::get('login', array('as'=>'login', 'uses'=>'UsersController@getLogin'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UsersController@postLogin'));

login.blade.php -我的登录页面

@if($errors->has())
        <p>The following errors have occured:</p>
        <ul id="form-errors">
            {{ $errors->first('username', '<li>:message</li>') }}
            {{ $errors->first('password', '<li>:message</li>') }}
        </ul>   
    @endif
    @if (Session::has('message'))
        <p>{{ Session::get('message') }}</p>
    @endif
    {{ Form::open(array('action'=>'login')) }}
    <p>
        {{ Form::label('username', 'Username') }}<br />
        {{ Form::text('username', Input::old('username')) }}
    </p>
    <p>
        {{ Form::label('password', 'Password') }}<br />
        {{ Form::password('password') }}
    </p>
    <p>
        {{ Form::submit('Login') }}
    </p>
    {{ Form::close() }}

UsersController.php (postCreate) -:您可以使用Hash 'password'=>Hash::make(Input::get('password')),

创建password

UsersController.php (postLogin):你正在尝试使用'password'=>Input::get('password')登录,所以这应该替换为

'password'=>Hash::make(Input::get('password'))

散列密码也需要64个字符的数据库字段。检查一下

我发现问题在于我允许输入数据库密码字段的字符数量。

我将密码列设置为:$table->string('password', 32); varchar(32),这不起作用,因为在laravel中散列密码至少需要64个字符。

将我的数据库中的密码列更改为varchar(64)修复了这个问题。