Laravel Auth ::尝试/登录页面无法正常工作

时间:2015-04-05 15:49:44

标签: laravel-4 login forms-authentication

我的注册页面工作正常.data被正确插入到名为" registered_users"的表中。我的登录页面无法正常工作。

routes.php文件

<?php



 Route::get('/', function()
{
    return View::make('index');
});


//About page
 Route::get('about', function()
{
    return View::make('about');
});

 //reister route

Route::get('register', function(){

return View::make('register');

});

Route::post('register_action', function()
{
        $obj = new RegisterController() ;
        return $obj->store();
});

Route::get('login', function(){

return View::make('login');

});


Route::post('logincheck', function(){

// username and password validation rule

    $data =  Input::except(array('_token')) ;
    $rule  =  array(
            'name'       => 'required',
            'password'   => 'required',
    ) ;
    $message = array(
       'password.required' => 'The Password field is required.',
       'name.required'      => 'The Username  is required.',
    );


    $v = Validator::make($data,$rule);





    if ($v->fails()) {

            // username or password missing

            // validation fails

            // used to retain input values

            Input::flash ();

            // return to login page with errors

            return Redirect::to('login')

            ->withInput()

            ->withErrors ($v->messages ());

    } else {

                    $userdata = array (

                    'name' => Input::get('name'),

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

                );

                //var_dump($userdata);
                If (Auth::attempt ($userdata)) {

        // authentication success, enters home page

         return Redirect::to('home');

     } else {

         // authentication fail, back to login page with errors

         return Redirect::to('login')
            ->withErrors('Incorrect login details');


         }//end if 




    }//end of v else

});


// Route::get ('home',function(){

//  return View::make('home');

// });
Route::get ('test',function(){

    return View::make('test');

    });

Route::group (array ('before' => 'auth'), function () {

    Route::get ('home',function(){

    return View::make('home');

    });

});
Route::get ('logout',function(){

Auth::logout ();

return Redirect::to('login');

});

我在 routes.php 中逐步检查了路由::帖子(&#39; logincheck&#39;,功能() ......一切正常。 ..但 routes.php Route :: post(&#39; logincheck&#39;,function()的部分内容无效。无论我输入正确登录表单中的名称和密码或错误,它只显示&#39;不正确的登录详细信息&#39;消息

If (Auth::attempt ($userdata)) {

        // authentication success, enters home page

         return Redirect::to('home');

     } else {

         // authentication fail, back to login page with errors

         return Redirect::to('login')
            ->withErrors('Incorrect login details');


         }//end if

login.blade.php

    @extends('layout')

    @section('main')
    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Registrationhhjh</title>
    </head>
    <body>

        <section class="header">
        <div class="bannner">&nbsp;</div>
        <div class="container">
            <h1>Sign In</h1>

            @if ($errors->any())

            <ul style="color:red;">

            {{ implode('', $errors->all('<li>:message</li>')) }}

            </ul>

            @endif

  {{ Form::open(array('url' => 'logincheck')) }}
             {{ Form::text('name', '', array('placeholder'=>'Name')) }}<br><br>
            {{ Form::password('password', '', array('placeholder'=>'Password')) }}<br><br>
            {{ Form::submit('Sign in', array('class'=>'btn btn-success')) }}
            <!-- {{ Form::submit('register', array('class'=>'btn btn-primary')) }} -->
            {{ HTML::link('register', 'Register', array('class' => 'btn btn-info'))}}

            {{ Form::close() }}



           </div>

        </section>





    </body>
    </html>

    @stop

User.php(模型).i认为User.php(模型)可能存在问题

<?php

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 = 'registered_users';

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

    public function getAuthIdentifier()
  {
    return $this->getKey();
  }
  public function getAuthPassword()
  {
    return $this->password;
  } 
  public function getRememberToken()
  {
    return $this->remember_token;
  }
  public function setRememberToken($value)
  {
    $this->remember_token = $value;
  }
    public function getRememberTokenName()
  {
    return "remember_token";
  }
  public function getReminderEmail()
  {
    return $this->email;
  }

}

auth.php ..任何人都可以告诉我什么是&#39; table&#39; =&GT; auto.php 中的用户&#39; ?....一旦我将其重命名为&#39; table&#39; =&GT; &#39; registered_users&#39;因为我没有&#39;用户&#39;命名表。但问题仍然存在(同样我不能进入&#39;主页&#39;)

<?php

return array(

    /*
    |--------------------------------------------------------------------------
    | Default Authentication Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the authentication driver that will be utilized.
    | This driver manages the retrieval and authentication of the users
    | attempting to get access to protected areas of your application.
    |
    | Supported: "database", "eloquent"
    |
    */

    'driver' => 'eloquent',

    /*
    |--------------------------------------------------------------------------
    | Authentication Model
    |--------------------------------------------------------------------------
    |
    | When using the "Eloquent" authentication driver, we need to know which
    | Eloquent model should be used to retrieve your users. Of course, it
    | is often just the "User" model but you may use whatever you like.
    |
    */

    'model' => 'User',

    /*
    |--------------------------------------------------------------------------
    | Authentication Table
    |--------------------------------------------------------------------------
    |
    | When using the "Database" authentication driver, we need to know which
    | table should be used to retrieve your users. We have chosen a basic
    | default value but you may easily change it to any table you like.
    |
    */

    'table' => 'users',

    /*
    |--------------------------------------------------------------------------
    | Password Reminder Settings
    |--------------------------------------------------------------------------
    |
    | Here you may set the settings for password reminders, including a view
    | that should be used as your password reminder e-mail. You will also
    | be able to set the name of the table that holds the reset tokens.
    |
    | The "expire" time is the number of minutes that the reminder should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'reminder' => array(

        'email' => 'emails.auth.reminder',

        'table' => 'password_reminders',

        'expire' => 60,

    ),

);

我使用wamp server..i使用laravel 4.2..my数据库名称是 mydb ...表名 的 registered_users 表格有 id 名称电子邮件密码 remember_token created_at updated_at 字段

remember_token 字段类型为varchar(270)... remember_token 大小是否有任何限制

请帮助我任何人登录系统中的问题是什么?

这是我的RegisterController.php

<?php

class RegisterController 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 store()
    // {
 //        Register::saveFormData(Input::except(array('_token')));
 //    }

    public function store()
    {
            $data =  Input::except(array('_token')) ;
            $rule  =  array(
                    'name'       => 'required|unique:registered_users',
                    'email'      => 'required|email|unique:registered_users',
                    'password'   => 'required|min:6|same:cpassword',
                    'cpassword'  => 'required|min:6'
                ) ;
            $message = array(
                   'cpassword.required' => 'The confirm password field is required.',
                   'cpassword.min'      => 'The confirm password must be at least 6 characters',
                   'password.same'      => 'The :attribute and confirm password field must match.',
               );
            $validator = Validator::make($data,$rule,$message);

           // $validator = Validator::make($data,$rule);

            if ($validator->fails())
            {
                    return Redirect::to('register')
                            ->withErrors($validator->messages());
            }
            else
            {
                    Register::saveFormData(Input::except(array('_token','cpassword')));

                    return Redirect::to('register')
                            ->withMessage('Successfully registered');
            }
    }

}

这是model / Register.php

<?php

class Register extends Eloquent {
        protected $guarded = array();
        protected $table = 'registered_users'; // database table name
        public $timestamps = 'false' ; // to disable default timestamp fields

        // model function to store form data to database
        public static function saveFormData($data)
        {
            DB::table('registered_users')->insert($data);
        }

}

最后我解决了我的问题。我的问题是我将密码存储为明文。为了解决我的问题,我刚编辑了我的models \ Register.php文件。 这是我编辑过的模型\ Register.php

<?php

class Register extends Eloquent {
        protected $guarded = array();
        protected $table = 'registered_users'; // database table name
        public $timestamps = 'false' ; // to disable default timestamp fields

        // model function to store form data to database
        public static function saveFormData($data)
        {
          //  DB::table('registered_users')->insert($data);
                $name = Input::get('name');
                $email = Input::get('email');
                $password = Hash::make(Input::get('password'));


                $user = new User;

                $user->name = $name;
                $user->email = $email;
                $user->password = $password;

                $user->save();
        }

}

1 个答案:

答案 0 :(得分:0)

Auth :: attempt()函数会自动对您的密码进行哈希处理。问题是您将密码存储为纯文本。因此,当方法检查您的密码是否正确时,它会对其进行哈希并将其与保存的密码进行比较。该密码没有哈希,因此除非您在注册表中哈希密码,否则它不会工作。