du2229 2015-03-03 07:51
浏览 95
已采纳

Auth :: attempt不检查用户名和密码

routes.php

//form login
Route::get('/', array('before' => 'guest', function()
{
    return View::make('login');
}));

// check login
Route::post('login', 'HomeController@validate');

HomeController.php

class HomeController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */

    public function validate() 
    {
        // set the remember me cookie if the user check the box
        $remember = (Input::has('remember')) ? true : false;
        // attempt to do the login

        $email = Input::get('username');
        $pass = Input::get('password');
        $password = Hash::check($remember,$pass);
        $credentials = array(
        'username' => "'$email'",
        'password' => "'$pass'"
        );
        $auth=Auth::attempt(array(
      'username' => "'$email'",
        'password' => "'$pass'",
));
        if($auth) {
             return 'success';
        }
        else {
             return 'auth failed';
        } 

    }

}

User.php

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {

    /**
     * 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;
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }

}

login.php

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <title>Simple Login</title>
    </head>
<body>
    <?php echo Form::open(array('url' => 'login', 'role' => 'form')) ?>
        <h2>Please sign in</h2>
        <!-- if there are login errors, show them here -->
        <p>
            <?php  if(Session::has('flash_notice')): ?>
                <div id="flash_notice"><?php echo Session::get('flash_notice') ?></div>
            <?php endif; ?>
        </p>
        <input type="text" name="username" value="<?php echo Input::old('username') ?>" required="required" autofocus="autofocus" autocomplete="off" placeholder="Username" />
        <input name="password" placeholder="Password" required="required" type="password" />
        <div>
            <input name="remember" type="checkbox" /> Remember me
        </div>
        <input type="submit" value="Sign in" />
    <?php echo Form::close() ?>
</body>
</html>

I am using laravel4 on my xamppp machine . I have made a simple login form in which a registered user can login in , to see the dashboard but When i use Auth::attempt to check the user is valid or not It will always shows me an error 'the username of password is wrong '

INSERT INTO `users` (`id`, `name`, `username`, `email`, `password`, `remember_token`, `created_at`, `updated_at`) VALUES
(3, 'Imron Rosdiana', 'imron02', 'imron@rosdiana.com', '$2y$10$bAwhqg41gHPOLD36aVxQi.ItviwlN663gCIt6S8H2VjeR8kZWHAZy', 'pvusxXobB9wAWKReXte5pv51vD4BKfPi4LjEV9JUlArLdmc4DL30eipAJ6Nb', '2014-04-22 16:45:57', '2014-04-28 03:45:26');

USERNAME:imron02 PASSWORD:123456 (by using hash::make i have inserted the password)

I know so many users have asked this question but still i am getting stuck at the same and everytime i get auth failed

  • 写回答

2条回答 默认 最新

  • drkjzk3359 2015-03-03 08:03
    关注

    Change this part:

    $credentials = array(
            'username' => "'$email'",
            'password' => "'$pass'"
            );
    $auth=Auth::attempt(array(
          'username' => "'$email'",
            'password' => "'$pass'",
    ));
    

    To this:

    $credentials = array(
            'username' => $email,
            'password' => $pass
            );
    $auth=Auth::attempt($credentials);
    

    Refer to this.

    But while doing this make sure you have a column corresponding to username in your table. I had to mention this because the username you provided does not seem to be an email id. The username and password combination should exist in the corresponding table in order to authenticate.

    If you are planning to take email id as the username you need to change this line:

    'username' => $email,
    

    to this:

    'email' => $email,
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?