使用Laravel 4验证定制的表名 [英] Use Laravel 4 Auth with custom table name

查看:339
本文介绍了使用Laravel 4验证定制的表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建基于现有数据库的新laravel 4后端。我有那么一个表名为FormMembers包含电子邮件地址和密码我的成员。

I am creating a new laravel 4 backend based on an existing database. I have then a table named 'FormMembers' that contains email and passwords for my members.

我想用验证::功能,如:

I would like to use Auth:: functions like :

$credentials = array(
    'sEmail' => Input::get('email'),
    'sPassword' => Input::get('password')
);

if (Auth::attempt($credentials)) {
    return Response::json(Auth::user());
} else {
    return Response::json(array('flash' => 'invalid password or email.'), 500);
}

所以我在改表字段的 auth.php 从用户到FormMembers并做了同样的用户模式。但调用验证::用户()时,我得到了错误:

So I changed the table field in auth.php from 'users' to 'FormMembers' and did the same to the Users model. But when calling Auth::user() I got the error :

{"error":{"type":"ErrorException","message":"Undefined index: password","file":"\/home\/www\/accred\/web\/vendor\/laravel\/framework\/src\/Illuminate\/Auth\/EloquentUserProvider.php","line":75}}

你有什么想法,我怎么能管理它?
谢谢

Do you have any idea how I could manage it? Thanks

推荐答案

的雄辩<一个href=\"https://github.com/laravel/framework/blob/master/src/Illuminate/Auth/EloquentUserProvider.php#L75\"相对=nofollow>用户提供程序需要一个 密码字段。你通过与键 spassword开头的数组。

The eloquent user provider requires a password field in the credentials array. You passed an array with the key sPassword.

您应该能够做到

$credentials = array(
    'password' => Input::get('email'),
    'sEmail' => Input::get('password'),
);

Auth::attempt($credentials);

和处理来自用户雄辩模型休息。即使你的用户表有不同的列。

And handle the rest from the User eloquent model. Even if your user table has different columns.

编辑:
该验证::尝试()函数来获取的被照亮的处理守卫类。
这就要求在 retrieveByCredentials ,然后实现类的 validateCredentials 方法 UserProviderInterface 。默认情况下,这是 EloquentUserProvider

The Auth::attempt() function get's handled by the Illuminate's Guard class. This calls the retrieveByCredentials and then the validateCredentials method of the class that implements the UserProviderInterface. By default this is the EloquentUserProvider.

retrieveByCredentials 方法使用之外的所有凭据阵列包含密码的钥匙。它返回一个用户雄辩的模型对象。因此,使用
你的'sEmail键那里。

The retrieveByCredentials method uses all except the password-containing keys of your credentials array. It returns one User eloquent model object. So use your 'sEmail' key there.

这是得到的呼吁雄辩用户提供者检查 validateCredentials 方法,如果密码字段的哈希凭据阵列从 getAuthPassword 方法检索您的用户对象上的散列密码匹配,至极需要实现,因为它实现了的UserInterface

The validateCredentials method that get's called on the eloquent user provider checks if the hash of the password field in the credentials array matches with the hashed password retrieved from the getAuthPassword method on your User object, wich it needs to implement, as it implements the UserInterface.

所以,你应该覆盖 getAuthPassword 方法上的用户类。

So, you should overwrite the getAuthPassword method on your User class.

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

喜欢这个返回存储在数据库中的哈希密码。

Like this it returns the hashed password stored in the DB.

Ofcourse密码应该以同样的方式作为哈希被散列::作出确实。如果不是:

Ofcourse your passwords should be hashed in the same way as Hash::make does. If not:


  1. 让所有用户重设密码

  2. 重新实现 BcryptHasher

这篇关于使用Laravel 4验证定制的表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆