草庐IT

php - Laravel Auth::attempt 每次都失败

coder 2024-04-10 原文

我在我的 laravel 应用程序上创建了一个测试用户。详情是

用户:joe@gmail.com 密码:123456

当我完成注册过程时,一切都按预期进行,并且在数据库用户表中创建了一个条目

完成后,我将用户重定向到仪表板。

public function postCreate(){
        //Rules
        $rules = array(
        'fname'=>'required|alpha|min:2',
        'lname'=>'required|alpha|min:2',
        'email'=>'required|email|unique:users',
        'password'=>'required|alpha_num|between:6,12|confirmed',
        'password_confirmation'=>'required|alpha_num|between:6,12'
        );
        
        $validator = Validator::make(Input::all(), $rules);
        if($validator->passes()){
            //Save in DB - Success
            $user = new User;
            $user->fname = Input::get('fname'); //Get the details of form
            $user->lname = Input::get('lname');
            $user->email = Input::get('email');
            $user->password = Hash::make(Input::get('password'));//Encrypt the password
            $user->save();
            return Redirect::to('/books')->with('Thank you for Registering!');
        }else{
            //Display error - Failed
            return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
        }
    }

然后我导航回登录页面并尝试使用上面的凭据登录,但我一直被告知 Auth::attempt() 失败,因此我的用户无法登录应用程序。

public function login(){
        if(Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))){
            //Login Success
            echo "Success"; die();
            return Redirect::to('/books');
        }else{
            //Login failed
            echo "Fail"; die();
            return Redirect::to('/')->with('message', 'Your username/password combination was incorrect')->withInput();
        }
    }

有人知道为什么会这样吗?这是我的 users 表的 Schema:

Schema::create('users', function($table){ 
            $table->increments('id'); 
            $table->integer('type')->unsigned(); 
            $table->string('fname', 255); 
            $table->string('lname', 255); 
            $table->string('email')->unique(); 
            $table->string('password', 60); 
            $table->string('school', 255); 
            $table->string('address_1', 255); 
            $table->string('address_2', 255); 
            $table->string('address_3', 255); 
            $table->string('address_4', 255);
            $table->string('remember_token', 100);
            $table->timestamps(); 
        });

非常感谢任何帮助。

'查看登录':

<div class="page-header">
    <h1>Home page</h1>
</div>

<!-- Register Form -->
<form   action="{{ action('UsersController@postCreate') }}" method="post" role="form">
    <h2 class="form-signup-heading">Register</h2>
    <!-- Display Errors -->
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>

    <!-- First Name -->
    <div class="form-group">
        <label>First Name</label>
        <input type="text" class="form-control" name="fname" /> 
    </div>
    <!-- Last Name -->
    <div class="form-group">
        <label>Last Name</label>
        <input type="text" class="form-control" name="lname" /> 
    </div>
    <!-- Email -->
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email" /> 
    </div>
    <!-- Password-->
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" />  
    </div>
    <!-- Confirm Password -->
    <div class="form-group">
        <label>Confirm Password</label>
        <input type="password" class="form-control" name="password_confirmation" /> 
    </div>
    <input type="submit" value="Register" class="btn btn-primary"/>
</form>

<!-- Login Form -->
<form   action="{{ action('UsersController@login') }}" method="post" role="form">
    <h2 class="form-signup-heading">Login</h2>
    <!-- Email -->
    <div class="form-group">
        <label>Email</label>
        <input type="text" class="form-control" name="email" /> 
    </div>
    <!-- Password-->
    <div class="form-group">
        <label>Password</label>
        <input type="password" class="form-control" name="password" />  
    </div>
    <input type="submit" value="Login" class="btn btn-primary"/>
</form>

最佳答案

你能在下面运行这个函数 - 并告诉我错误发生在哪里吗?它将诊断问题:

public function testLogin()
{
     $user = new User;
     $user->fname = 'joe';
     $user->lname = 'joe';
     $user->email = 'joe@gmail.com';
     $user->password = Hash::make('123456');

     if ( ! ($user->save()))
     {
         dd('user is not being saved to database properly - this is the problem');          
     }

     if ( ! (Hash::check('123456', Hash::make('123456'))))
     {
         dd('hashing of password is not working correctly - this is the problem');          
     }

     if ( ! (Auth::attempt(array('email' => 'joe@gmail.com', 'password' => '123456'))))
     {
         dd('storage of user password is not working correctly - this is the problem');          
     }

     else
     {
         dd('everything is working when the correct data is supplied - so the problem is related to your forms and the data being passed to the function');
     }
}

编辑:一个想法 - 您确定用户正确保存在数据库中吗?您是否尝试过“清空/删除”您的数据库并再次尝试您的代码?在您当前的代码中,如果您继续使用 joe@gmail.com 注册,它将失败——因为它是唯一的。但是你不会在任何地方发现错误。所以清空数据库再试一次...

编辑 2:I found another question you posted with the same problem - 您在其中提到以下代码是您的用户模型?

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

class User extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 

/** 
* 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'); 

public function getAuthIdentifier() { 

} 

public function getAuthPassword() { 
} 

public function getRememberToken() { 

} 

public function getRememberTokenName() { 

} 

public function getReminderEmail() { 

} 

public function setRememberToken($value) { 

} 
}

这正是您当前的用户模型吗?因为如果是这样 - 这是错误的 - 这些功能都不应该是空白的。

这就是 Laravel 4.2 的正确用户模型应该是什么样子

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

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * 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', 'remember_token');

}

关于php - Laravel Auth::attempt 每次都失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329584/

有关php - Laravel Auth::attempt 每次都失败的更多相关文章

  1. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  2. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  3. ruby-on-rails - 创建 ruby​​ 数据库时惰性符号绑定(bind)失败 - 2

    我正在尝试在Rails上安装ruby​​,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf

  4. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  5. ruby - 使用 rbenv 和 ruby​​-build 构建 ruby​​ 失败,出现 undefined symbol : SSLv2_method - 2

    我正在尝试在配备ARMv7处理器的SynologyDS215j上安装ruby​​2.2.4或2.3.0。我用了optware-ng安装gcc、make、openssl、openssl-dev和zlib。我根据README中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby​​-build插件。.这些是随optware-ng安装的软件包及其版本binutils-2.25.1-1gcc-5.3.0-6gconv-modules-2.21-3glibc-opt-2.21-4libc-dev-2.21-1libgmp-6.0.0a-1libmpc-1.0.2-1libm

  6. ruby-on-rails - Ruby 的 'open_uri' 是否在读取或失败后可靠地关闭套接字? - 2

    一段时间以来,我一直在使用open_uri下拉ftp路径作为数据源,但突然发现我几乎连续不断地收到“530抱歉,允许的最大客户端数(95)已经连接。”我不确定我的代码是否有问题,或者是否是其他人在访问服务器,不幸的是,我无法真正确定谁有问题。本质上,我正在读取FTPURI:defself.read_uri(uri)beginuri=open(uri).readuri=="Error"?nil:urirescueOpenURI::HTTPErrornilendend我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施来关闭所有连接,这样我的连接就不是问题所在,但是我

  7. ruby-on-rails - Ruby 流量控制 : throw an exception, 返回 nil 还是让它失败? - 2

    我在思考流量控制的最佳实践。我应该走哪条路?1)不要检查任何东西并让程序失败(更清晰的代码,自然的错误消息):defself.fetch(feed_id)feed=Feed.find(feed_id)feed.fetchend2)通过返回nil静默失败(但是,“CleanCode”说,你永远不应该返回null):defself.fetch(feed_id)returnunlessfeed_idfeed=Feed.find(feed_id)returnunlessfeedfeed.fetchend3)抛出异常(因为不按id查找feed是异常的):defself.fetch(feed_id

  8. ruby - gem 规范失败 - 2

    我正在为毕业设计开发GEM,TravisCI构建不断失败。这是我在Travis上的链接:https://travis-ci.org/ricardobond/perpetuus/builds/8709218构建错误是:$bundleexecrakerakeaborted!Don'tknowhowtobuildtask'default'/home/travis/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in`eval'/home/travis/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_

  9. ruby-on-rails - "rails generate rspec:install"似乎失败了 - 2

    运行:ruby1.9.3p0和Rails3.2.1尝试使用rspec但当我尝试将其安装到我的应用程序中时出现以下错误:/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.1/lib/rails/railtie/configuration.rb:85:in`method_missing':undefinedmethod`generators'for#(NoMethodError)from/Users/Si/.rvm/gems/ruby-1.9.3-p0/gems/rspec-rails-2.0.0.beta.18/lib/rspec-r

  10. ruby - Date 与 nil 的比较失败 - ruby - 2

    我正在运行这样的代码:ifvalid_from>Date.today当我运行它时,我得到一个错误提示comparisonofDatewithnilfailed我假设它正在发生,因为在某些情况下valid_from是nil。有没有办法避免出现此错误? 最佳答案 你可以这样做:ifvalid_fromandvalid_from>Date.today...end这将在第一个子句上短路,因为valid_from为nil,因此为false。 关于ruby-Date与nil的比较失败-ruby,我们

随机推荐