【问题标题】:auth:: attempt() always returns falseauth::attempt() 总是返回 false
【发布时间】:2014-03-15 16:39:29
【问题描述】:

我正在研究 Laravel,但我的身份验证系统似乎存在一些问题。我将尝试在下面对我的代码进行 sn-ps。如果我的解释还不够,请告诉我。

路线:

/*
 Sign in (POST)
 */
Route::post('/account/sign-in', array(
        'as' => 'account-sign-in-post',
        'uses' => 'AccountController@postSignIn'
));

/*
 Sign in (GET)
 */
Route::get('/account/sign-in', array(
    'as' => 'account-sign-in',
    'uses' => 'AccountController@getSignIn'
));

AccountController.php

<?php

class AccountController extends BaseController {

public function getSignIn() {
        return View::make('account.signin');
}

public function postSignIn() {
        $validator = Validator::make(Input::all(),
            array(
                'email'     => 'required|email',
                'password'  => 'required'
                )
            );

        if($validator->fails()) {
            //Redirect to sign in page
            return Redirect::route('account-sign-in')
            ->withErrors($validator)
            ->withInput();
        } else {
            //Atempt user sign in

            $auth = array(
                'email' => Input::get('email'),
                'password' => Input::get('password'),
                'active' => 1
                );  


            if(Auth::attempt($auth)) {
                //Redirect to intended page
                return Redirect::intended('/');
            }
            else {



                return Redirect::route('account-sign-in')
                     ->with('global', 'Email/password wrong, or                           account not activated');


            }
        }

        return Redirect::route('account-sign-in')
        ->with('global', 'There is a problem signing you in');
}

public function getCreate(){
    return View::make('account.create');
}

public function postCreate(){
    $validator = Validator::make(Input::all(), 
        array(
            'email'         =>  'required|max:50|email|unique:users',
            'username'      => 'required|max:20|min:3|unique:users',
            'password'      => 'required|min:6',
            'password_again'=> 'required|same:password'
            )
        );

    if($validator->fails())
    {
        return Redirect::route('account-create')
        ->withErrors($validator)
        ->withInput();
    }
    else
    {
        $email      = Input::get('email');
        $username   = Input::get('username');
        $password   = Input::get('password');

        // Activation code
        $code       = str_random(10);

        $user = User::create(array(
                'email'     => $email,
                'username'  => $username,
                'password'  => Hash::make($password),
                'code'      => (string)$code,
                'active'    => 0
            ));


    }
}


    return Redirect::route('home')
    ->with('global','Account could not be activated. Please, try again later.');
}
    }

?>

auth.php

<?php
  return array(
    'driver' => 'eloquent',
    'model' => 'User',
    'table' => 'users',
    'reminder' => array(
      'email' => 'emails.auth.reminder',
      'table' => 'password_reminders',
      'expire' => 60,
    ),
  );
?>

user.php

   <?php

    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;

   class User extends Eloquent implements UserInterface, RemindableInterface {

protected $fillable = array('email' , 'username' , 'password', 'code');
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

    }

signin.blade.php

      @extends('layout.main')

       @section('content')
<form action="{{ URL::route('account-sign-in-post') }}" method="post">

    <div class "field">
        Email: <input type="text" name="email"{{ (Input::old('email')) ? '  value="' . Input::old('email') . '"' : ''}}>
        @if($errors->has('email'))
            {{ $errors->first('email') }}
        @endif
    </div>

    <div class "field">
        Password: <input type="text" name="password">
        @if($errors->has('password'))
        {{ $errors->first('password') }}
        @endif
    </div>

    <input type="submit" value = "Sign in">
    {{ Form::token() }}


</form>
     @stop

结论:我对密码进行哈希处理,并将其存储在哈希处理后的数据库中。我正确使用了function Auth::attempt(),而没有重新设置密码。我见过有人使用Auth::attemptHash::make($password)。 auth.php 和 User.php 文件看起来不错。我不知道问题可能出在哪里。

【问题讨论】:

  • 您是否通过将active 设置为1 来激活用户?
  • 账户已激活,活动列设置为1。

标签: php authentication hash laravel passwords


【解决方案1】:

数据库中密码字段的长度必须为 60 或更高。

【讨论】:

  • 就是这样!我数据库中的密码列是 varchar(50)。我无缘无故地查看我的代码。所以记住孩子们,数据库中的密码字段必须是 60 或更高。
猜你喜欢
  • 2012-10-25
  • 1970-01-01
  • 2016-09-12
  • 2017-05-24
  • 2013-06-11
  • 2015-12-10
  • 1970-01-01
  • 2019-04-14
相关资源
最近更新 更多