Laravel Auth ,登录不起作用。它总是返回false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18965991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Auth , login doesn't work. It always return false?
提问by Ashley Lashley
I'm trying to make an Auth and Login application with Laravel but it always fails. I always see "Username or password incorrect." in login.blade.php
我正在尝试使用 Laravel 制作一个身份验证和登录应用程序,但它总是失败。我总是看到“用户名或密码不正确”。在 login.blade.php 中
My files are like theese ;
我的文件就像这些一样;
route.php
路由文件
Route::get('login' , function() {
return View::make('auth.login');
});
Route::post('logincontrol', function() {
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if ( Auth::attempt($userdata) )
{
return Redirect::to('auth.dashboard');
}
else
{
return Redirect::to('login')
->with('login_errors', true);
}
});
Route::get('dashboard', function(){
echo 'welcome';
});
Route::get('insert', function() {
DB::table('users')->insert(array(
'username' => 'admin',
'password' => Hash::make('admin')
));
});
login.blade.php
登录名.blade.php
{{ Form::open( array('url' => 'logincontrol') ) }}
<!-- check for login errors flash var -->
@if (Session::has('login_errors'))
<span class="error">Username or password incorrect.</span>
@endif
<!-- username field -->
<p>{{ Form::label('username', 'Username') }}</p>
<p>{{ Form::text('username') }}</p>
<!-- password field -->
<p>{{ Form::label('password', 'Password') }}</p>
<p>{{ Form::password('password') }}</p>
<!-- submit button -->
<p>{{ Form::submit('Login') }}</p>
{{ Form::close() }}
First of all, i run the insert URL and added admin to my db.
首先,我运行插入 URL 并将管理员添加到我的数据库中。
Then i run /login URL form is shown. I enter username and password like in insert Route, but i always turn false.
然后我运行 /login URL 表单显示。我输入用户名和密码就像在插入路由中一样,但我总是变成假的。
i took the codes from http://codehappy.daylerees.com/authentication, i read all of the page but i could not do it .
我从http://codehappy.daylerees.com/authentication 获取了代码,我阅读了所有页面,但我做不到。
Edit : I tried ;
编辑:我试过了;
'password' => Hash::make( Input::get('password') )
But it didn't work :(
但它没有用:(
Edit: my auth.php file is like this.
编辑:我的 auth.php 文件是这样的。
return array(
'driver' => 'eloquent',
'username'=>'username',
'model' => 'User',
'table' => 'users',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
);
回答by mXX
The Hash that is always changing is normal because the Hash::check()'ing process uses the salt embedded in the hash to compare the plain text value. for more information about this look here http://laravel.io/topic/20/hashing-in-laraveland here http://en.wikipedia.org/wiki/Salt_(cryptography)
一直在变化的哈希是正常的,因为Hash::check()'ing 过程使用嵌入在哈希中的salt 来比较纯文本值。有关此的更多信息,请查看这里 http://laravel.io/topic/20/hashing-in-laravel和这里http://en.wikipedia.org/wiki/Salt_(cryptography)
I've stumbled upon your question because I'm having the exact error...
我偶然发现了你的问题,因为我有确切的错误......
I'm recommending you to watch this tutorialby Jeffrey Way about Authentication. If I find something I'll let you know because I'm also struggling with this problem at the moment.
我建议您观看Jeffrey Way 关于身份验证的教程。如果我发现了什么,我会告诉你的,因为我目前也在为这个问题而苦苦挣扎。
回答by Ossi
Sometimes it is the simple things that get you. I spend a large part of today with a similar issue and the solution in my case was that I am using artisan generate:scaffold which had created the user model with;
有时,是简单的事情让您受益。我今天的大部分时间都在解决类似的问题,在我的情况下,我的解决方案是使用 artisan generate:scaffold 来创建用户模型;
class User extends Eloquent
class User extends Eloquent
I modified this model by adding
我通过添加修改了这个模型
implements UserInterface, RemindableInterface
implements UserInterface, RemindableInterface
and then added the following missing functions:
然后添加了以下缺失的功能:
/**
* 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;
}
Everything works just fine for me now. Hope this helps you.
现在一切对我来说都很好。希望这对你有帮助。
ossi
奥西
回答by The Alpha
Configuration for authentication
could be found in application/config/auth.php
and in this file, there is a setting for the the username
like
authentication
可以application/config/auth.php
在这个文件中找到配置,有一个username
类似的设置
/*
|--------------------------------------------------------------------------
| Authentication Username
|--------------------------------------------------------------------------
|
| Here you may specify the database column that should be considered the
| "username" for your users. Typically, this will either be "username"
| or "email". Of course, you're free to change the value to anything.
|
*/
By default it's
默认情况下是
'username' => 'email',
In your case, it's username
because you have
就你而言,那是username
因为你有
DB::table('users')->insert(array(
'username' => 'admin', // looks like you are using username for login
'password' => Hash::make('admin')
));
And following code for login
以及以下登录代码
$userdata = array(
'username' => Input::get('username'), // you are using username field
'password' => Input::get('password')
);
// Login
if ( Auth::attempt($userdata) )
{
//...
}
So, if you didn't change it in the application/config/auth.php
file then you should change it to
因此,如果您没有在application/config/auth.php
文件中更改它,那么您应该将其更改为
'username' => 'username',
Alternatively you can us an email
field in your table and authenticate using email
field instead of username
. BTW, this is related to Laravel-3
according to your given linkin the question.
或者,您可以email
使用表中的一个字段并使用email
field 而不是 进行身份验证username
。顺便说一句,这与Laravel-3
您在问题中给出的链接有关。