dqwh1219 2013-12-28 00:20
浏览 30
已采纳

Laravel - 登录保持呼应FAIL

I'm using the framework Laravel.

I have 2 tables (Users and Persons). I want to check if the user_email and user_password are in the database, if so I want to login.

Table Users

CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
  `user_id` BIGINT NOT NULL AUTO_INCREMENT,
  `user_username` VARCHAR(45) NOT NULL,
  `user_email` VARCHAR(45) NOT NULL,
  `user_password` CHAR(64) NOT NULL,
  `user_salt` CHAR(32) NOT NULL,
  `user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_modified` TIMESTAMP NULL,
  `user_deleted` TIMESTAMP NULL,
  `user_lastlogin` TIMESTAMP NULL,
  `user_locked` TIMESTAMP NULL,
  `user_token` VARCHAR(128) NULL,
  `user_confirmed` TIMESTAMP NULL,
  PRIMARY KEY (`user_id`, `person_id`),
  UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
  INDEX `fk_users_persons1_idx` (`person_id` ASC),
  CONSTRAINT `fk_users_persons1`
    FOREIGN KEY (`person_id`)
    REFERENCES `festival_aid`.`persons` (`person_id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Table Persons

CREATE TABLE IF NOT EXISTS `festival_aid`.`persons` (
  `person_id` BIGINT NOT NULL AUTO_INCREMENT,
  `person_firstname` VARCHAR(45) NULL,
  `person_surname` VARCHAR(45) NULL,
  `person_created` TIMESTAMP NOT NULL,
  `person_modified` TIMESTAMP NULL,
  `person_deleted` TIMESTAMP NULL,
  PRIMARY KEY (`person_id`))
ENGINE = InnoDB;

User migration

Schema::table('users', function(Blueprint $table)
        {
            $table->increments('user_id');
            $table->string('user_email');
            $table->string('user_password', 64);
            $table->timestamp('user_created');
            $table->timestamp('user_modified');
            $table->timestamp('user_deleted');
            $table->timestamp('user_lastlogin');
            $table->timestamp('user_locked');

            $table->foreign('person_id')
                ->references('id')->on('persons')
                ->onDelete('cascade');
        });

Person migration

public function up()
    {
        Schema::table('persons', function(Blueprint $table)
        {
            $table->increments('person_id');

            $table->string('person_firstname');
            $table->string('person_surname');
        });
    }

Model User

class User extends Eloquent  {

    protected $primaryKey = 'user_id';

    protected $guarded = array();

    public static $rules = array();

    protected $table = 'users';

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getReminderEmail()
    {
        return $this->user_email;
    }

    public function getPasswordAttribute()
    {
        return $this->user_password;
    }

    public function persons()
    {
        return $this->hasOne('Person', 'person_id', 'user_id');
    }
}

Model Person

class Person extends Eloquent {

    protected $table = 'persons';

    protected $primaryKey = 'person_id';

    protected $guarded = array();

    public static $rules = array();

    public function users()
    {
        return $this->belongsTo('User', 'user_id', 'person_id');
    }

    public $timestamps = false;
}

LoginController

public function showLogin()
{
    return View::make('login');
}

public function doLogin()
{
    $rules = array(
    'user_email'    => 'required|email', 
    'user_password' => 'required'
);

$validator = Validator::make(Input::all(), $rules);

if ($validator->fails()) {
    return Redirect::to('login')
        ->withErrors($validator) 
        ->withInput(Input::except('user_password')); 
} else {

    $userdata = array(
        'user_email'    => Input::get('user_email'),
        'password'  => Input::get('user_password')
    );

    if (Auth::attempt($userdata)) {
        echo 'SUCCESS!';
    } else {
        echo 'FAIL!';
    }
}

Login view

@extends('layouts.master')
@section('content')
{{ Form::open(array('url' => 'login')) }}
<h1>Login</h1>
<p>
    {{ $errors->first('user_email') }}
    {{ $errors->first('user_password') }}
</p>
<p>
    {{ Form::label('email', 'Email Address') }}
    {{ Form::text('user_email', Input::old('user_email'), array('placeholder' => 'email')) }}
</p>
<p>
    {{ Form::label('password', 'Password') }}
    {{ Form::password('user_password') }}
</p>
<p>{{ Form::submit('Submit!') }}</p>
{{ Form::close() }}
@stop

Login routes

Route::get('login', array('uses' => 'LoginController@showLogin'));

Route::post('login', array('uses' => 'LoginController@doLogin'));

auth.php file

return array(
    'driver' => 'eloquent',

    'model' => 'User',

    'table' => 'users',

    'reminder' => array(

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

        'table' => 'password_reminders',

        'expire' => 60,
    ),
);

The problem is that it always echoes back "FAIL" and I want to see success for a change, does someone know if I did something wrong? Is it because there is a relation: one to one between persons and users that it doesn't work?

  • 写回答

1条回答 默认 最新

  • dousong9729 2013-12-28 00:59
    关注

    Your User model must implements at least UserInterface

    <?php
    
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;
    
    class User extends Eloquent implements UserInterface, RemindableInterface {
     ...
    }
    

    And your getAuthPassword() must return the correct password:

    public function getAuthPassword()
    {
        return $this->user_password;
    }
    

    Laravel will look for password key in the credentials array:

    $userdata = array(
        'user_email'    => Input::get('user_email'),
        'password'     => Input::get('user_password')
    );
    
    if (Auth::attempt($userdata)) {
        echo 'SUCCESS!';
    } else {
        echo 'FAIL!';
    }
    

    Here's the related code in Illuminate\Auth\DatabaseUserProvider.php:

    public function validateCredentials(UserInterface $user, array $credentials)
    {
        $plain = $credentials['password'];
    
        return $this->hasher->check($plain, $user->getAuthPassword());
    }
    

    Create an getter on your User model, so Guard can get the password using password attribute:

    public function getPasswordAttribute()
    {
       return $this->user_password;
    }
    

    And your password field should be at least 60 chars long:

    `user_password` CHAR(60) NOT NULL,
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)