【问题标题】:Laravel auth not working using correct username/password combinationLaravel 身份验证无法使用正确的用户名/密码组合
【发布时间】:2015-10-20 22:44:02
【问题描述】:

由于某种原因,我无法使用正确的用户名/密码组合登录。当登录信息不正确时,我正在使用 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() }}

【问题讨论】:

    标签: php laravel authentication


    【解决方案1】:

    在你的

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

    创建

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

    'password'=&gt;Hash::make(Input::get('password'))

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

    【讨论】:

    • 经过进一步检查,我发现在登录时对密码进行哈希处理并不是正确的做法。我原以为它可以工作,因为它重定向成功,但事实并非如此。它不起作用的原因是因为我将密码字段设置为 varchar(32) 并且哈希密码需要 64。您应该更新您的答案。
    【解决方案2】:

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

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

    将我的数据库中的密码列更改为 varchar(64) 解决了该问题。

    【讨论】:

    • 有趣的是,这对我不起作用。我使用的是 24,但切换到 64 后没有任何变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多