[英]How to login with hash class in Laravel 5.1
I want use hash class for hash my passwords. 我想使用哈希类来哈希我的密码。
because I want slat password with My policy. 因为我要在“我的政策”中添加密码。
I have controller like this: 我有这样的控制器:
public function postLogin(Request $request)
{
$rules = array(
'username' => 'required',
'password' => 'required|min:8'
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)
->withInput();
} else {
$user=User::where('username',$request['username'])->where('group_id',1)->get()->toArray();
$password=new PasswordController;
$request['password'] = $password->create_password_str($request['username'],$request['password']);
if(isset($user[0]['password'])&&Hash::check($request['password'], $user[0]['password'])){
return redirect('/dashboard');
} else {
return view('auth.login')->with('flag',5);
}
}
}
I use username after login like this 我这样登录后使用用户名
{{Auth::user()->username}}
show me error 告诉我错误
Trying to get property of non-object 试图获取非对象的属性
I add this code before redirect but dont work. 我在重定向之前添加了此代码,但不起作用。
Auth::login($request['username']);
Because you do not use the laravel builtin function so it did not store the user in a SESSION
and it throws an error while you try to fetch a user data which actually does not exists. 因为您没有使用laravel内置函数,所以它没有将用户存储在SESSION
并且在您尝试获取实际上不存在的用户数据时会引发错误。 Here's how to authenticate with laravel auth attempt function 这是使用laravel身份验证尝试功能进行身份验证的方法
if( Auth::attempt(['username' => $username, 'password' => $password]))
{
//redirect to dashboard
} else
{
//invalid credentials
}
Note : Even you do not need to convert your password to hash. 注意:即使您不需要将密码转换为哈希。 laravel is intelligent enough to convert it to hash. laravel足够聪明,可以将其转换为哈希。
Update: If you want to use custom salt for your project you could checkout this link . 更新:如果您想为项目使用自定义盐,则可以签出此链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.