我正在使用Laravel框架。
我有2个表(用户和人员)。我想检查user_email和user_password是否在数据库中,如果是,我想登录。
表用户
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;
表人
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;
用户迁移
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');
});
人员迁移
public function up()
{
Schema::table('persons', function(Blueprint $table)
{
$table->increments('person_id');
$table->string('person_firstname');
$table->string('person_surname');
});
}
模型用户
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');
}
}
模特人
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!';
}
}
登录视图
@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
登录路线
Route::get('login', array('uses' => 'LoginController@showLogin'));
Route::post('login', array('uses' => 'LoginController@doLogin'));
auth.php文件
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
问题在于它总是回应“失败”,我希望看到改变成功,有人知道我做错了什么吗?是因为存在一种关系:人和用户之间的一对一关系不起作用?
答案 0 :(得分:2)
您的用户模型必须至少实现UserInterface
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
...
}
您的getAuthPassword()必须返回正确的密码:
public function getAuthPassword()
{
return $this->user_password;
}
Laravel将在凭证数组中查找密码密钥:
$userdata = array(
'user_email' => Input::get('user_email'),
'password' => Input::get('user_password')
);
if (Auth::attempt($userdata)) {
echo 'SUCCESS!';
} else {
echo 'FAIL!';
}
以下是Illuminate\Auth\DatabaseUserProvider.php
中的相关代码:
public function validateCredentials(UserInterface $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
在您的用户模型上创建一个getter,因此Guard可以使用password
属性获取密码:
public function getPasswordAttribute()
{
return $this->user_password;
}
您的密码字段应至少为60个字符:
`user_password` CHAR(60) NOT NULL,