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?