使用 Laravel 通过用户名和密码获取用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26409806/
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
Get a user by username and password with Laravel
提问by Jimmyt1988
I need to do some extra checks on a user, I would like to get the user by username and password.
我需要对用户进行一些额外检查,我想通过用户名和密码获取用户。
Firstly:
首先:
Is there a built in function that gets a user by username and password without authenticating them?
是否有内置功能可以通过用户名和密码获取用户而不对其进行身份验证?
Secondly:
其次:
If the above is no, then how do I correctly hash the password, because if I use Hash::make( $password )
and then compare to the database, it is not the same... You would usually use Hash::check but I need to actually get the user by username and password.
如果以上是否定的,那么我如何正确地散列密码,因为如果我使用Hash::make( $password )
然后与数据库进行比较,它就不一样了......你通常会使用 Hash::check 但我需要实际获取用户通过用户名和密码。
采纳答案by Marcin Nabia?ek
First:
第一的:
If you want to check if user data to authentication is correct you can use:
如果要检查要进行身份验证的用户数据是否正确,可以使用:
if (Auth::validate($credentials))
{
//
}
But if you want to get user from database with user and password, you can use:
但是如果你想用用户名和密码从数据库中获取用户,你可以使用:
$user = User::whereName($username)->wherePassword(Hash::make($password))->first();
Second
第二
To store password in database you should use Hash::make($password)
as you showed and it works without any problems. Using Auth::validate
should solve the issue.
要将密码存储在数据库中,您应该Hash::make($password)
像您展示的那样使用,并且它可以正常工作。使用Auth::validate
应该可以解决问题。
回答by kamal pal
In Laravel 5.2
在 Laravel 5.2 中
You can use Auth::once($credentials)
to validate credentials and thereafter Auth::getUser();
to get the user.
您可以使用Auth::once($credentials)
验证凭据,然后Auth::getUser();
获取用户。
$credentials = Request::only('username', 'password');
if(!Auth::once($credentials)) {
// Invalid user credentials; throw Exception.
}
$user = Auth::getUser();
回答by JasonK
Yes, there is a built in function you should use. I recommend you to read the docs. But here's a good example, it's pretty self-evident:
是的,您应该使用一个内置函数。我建议你阅读文档。但这是一个很好的例子,它是不言而喻的:
$input = array(
'username' => Input::get('username'),
'password' => Input::get('password'),
);
$remember = (boolean)Input::get('remember'); //'Remember me' checkbox
if (Auth::attempt($input, $remember)) {
return Redirect::intended('dashboard')->with("success", "You're logged in!"); //Redirect the user to the page intended to go to, with the dashboard page as default
}
Registering a user looks something like this:
注册用户看起来像这样:
$input = array(
'username' => Input::get('username'),
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password')) //Encrypt password
);
$user = User::create($input);
I also recommend you to read about input validation. I hope this helps, good luck.
我还建议您阅读有关输入验证的信息。我希望这会有所帮助,祝你好运。
Edit: I didn't read the "without authenticating them" part. You should use Auth::validate($input)
as Marcin already explained.
编辑:我没有阅读“未经验证”部分。您应该Auth::validate($input)
像 Marcin 已经解释过的那样使用。
回答by Robert Stanley
Laravel 5.7
Laravel 5.7
To check a users credentials without logging them in, I had to do this:
要在不登录的情况下检查用户凭据,我必须这样做:
$user = User::whereEmail($request->email)->first();
$user = password_verify($request->password, optional($user)->getAuthPassword()) ? $user : false;
Laravel auth validation makes use of https://www.php.net/manual/en/function.password-verify.php
Laravel 身份验证使用https://www.php.net/manual/en/function.password-verify.php