我正在挖掘Laravel,看来我的身份验证系统存在一些问题。我将尝试在下面制作我的代码片段。如果我的解释不够,请告诉我。
路线:
/*
Sign in (POST)
*/
Route::post('/account/sign-in', array(
'as' => 'account-sign-in-post',
'uses' => 'AccountController@postSignIn'
));
/*
Sign in (GET)
*/
Route::get('/account/sign-in', array(
'as' => 'account-sign-in',
'uses' => 'AccountController@getSignIn'
));
AccountController.php
<?php
class AccountController extends BaseController {
public function getSignIn() {
return View::make('account.signin');
}
public function postSignIn() {
$validator = Validator::make(Input::all(),
array(
'email' => 'required|email',
'password' => 'required'
)
);
if($validator->fails()) {
//Redirect to sign in page
return Redirect::route('account-sign-in')
->withErrors($validator)
->withInput();
} else {
//Atempt user sign in
$auth = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'active' => 1
);
if(Auth::attempt($auth)) {
//Redirect to intended page
return Redirect::intended('/');
}
else {
return Redirect::route('account-sign-in')
->with('global', 'Email/password wrong, or account not activated');
}
}
return Redirect::route('account-sign-in')
->with('global', 'There is a problem signing you in');
}
public function getCreate(){
return View::make('account.create');
}
public function postCreate(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'password_again'=> 'required|same:password'
)
);
if($validator->fails())
{
return Redirect::route('account-create')
->withErrors($validator)
->withInput();
}
else
{
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('password');
// Activation code
$code = str_random(10);
$user = User::create(array(
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => (string)$code,
'active' => 0
));
}
}
return Redirect::route('home')
->with('global','Account could not be activated. Please, try again later.');
}
}
?>
auth.php
<?php
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
?>
user.php的
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array('email' , 'username' , 'password', 'code');
/**
* 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;
}
}
signin.blade.php
@extends('layout.main')
@section('content')
<form action="{{ URL::route('account-sign-in-post') }}" method="post">
<div class "field">
Email: <input type="text" name="email"{{ (Input::old('email')) ? ' value="' . Input::old('email') . '"' : ''}}>
@if($errors->has('email'))
{{ $errors->first('email') }}
@endif
</div>
<div class "field">
Password: <input type="text" name="password">
@if($errors->has('password'))
{{ $errors->first('password') }}
@endif
</div>
<input type="submit" value = "Sign in">
{{ Form::token() }}
</form>
@stop
总之:我将密码哈希并存储在数据库哈希中。我正确使用function Auth::attempt()
而不重新密码。我看到有人使用Auth::attempt
和Hash::make($password)
。 auth.php和User.php文件似乎很好。我不知道问题出在哪里。
答案 0 :(得分:1)
数据库中密码字段的长度必须为60或更高。