php - Laravel - 似乎无法使用种子用户登录
问题描述
我正在用 Laravel 做一个 SPA,我有一个播种机供我的用户使用。播种机似乎很好地播种了我的数据库。对于每个用户,密码与我使用 Hash:make 设置的 12345678 相同。但是当我尝试登录时,当用户清楚地在数据库中时它不起作用(无法检查密码,因为它是散列的)。于是我尝试注册了一些用户,奇怪的是注册的用户可以正常登录。我的想法很震惊,我已经看了好几天了,所以是时候寻求帮助了。
我检查了请求的有效负载,它与输入完全匹配。我很确定它与播种机有关,但我无法弄清楚。它过去也可以很好地处理播种数据,但我不再拥有旧播种机。
数据库播种机
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// Seed the users
$this->call(UserSeeder::class);
//seed the campaigns
$this->call(CampaignSeeder::class);
//seed the campaign users
$this->call(UserCampaignSeeder::class);
}
}
用户播种机
use App\User;
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$user = new User();
$user->name = "Test1";
$user->email = "test1@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test2";
$user->email = "test2@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test3";
$user->email = "test3@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test4";
$user->email = "test4@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test5";
$user->email = "test5@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test6";
$user->email = "test6@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test7";
$user->email = "test7@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
$user = new User();
$user->name = "Test8";
$user->email = "test8@gmail.com";
$user->password = Hash::make("12345678");
$user->save();
}
}
认证控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use App\User;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login', 'register']]);
}
public function register(Request $request)
{
$user = User::create([
'name' => $request->username,
'email' => $request->email,
'password' => $request->password,
]);
$token = auth('api')->login($user);
return $this->respondWithToken($token);
}
/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);
if (!$token = auth('api')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
/**
* Get the authenticated User.
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json(auth('api')->user());
}
/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth('api')->refresh());
}
/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'user' => $this->guard()->user(),
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60
]);
}
public function guard()
{
return Auth::Guard('api');
}
}
解决方案
我认为问题在于您没有正确导入Hash
播种机。但是,您可以简单地使用bcrypt()
而不是Hash
. 请试试:
$user->password = bcrypt("12345678");
请看看它是否有效。
此外,请确保您的 User 模型没有自动散列密码的 mutator 函数,正如 @ceejayoz 在评论中出色指出的那样 - 如果是这种情况,只需使用纯文本密码为用户播种。
推荐阅读
- javascript - Flatpickr onchange 事件将 dateStr 设置为 -1 天
- amazon-web-services - 无法在具有管理员权限的 AWS 上创建 lambda 函数
- android - 以本机反应将视频上传到firebase存储
- docusignapi - Docusign C# SDK ,更新收件人及其自定义字段
- reactjs - React & Redux: Infinite Scroll 导致 TypeError: Dispatch is not a Function 错误
- html - 如何对齐列,使它们都在一行中?HTML
- javascript - 如何在井字游戏中将段落值返回到 HTML 中?
- mongodb - MongoDB 聚合中 $lookup 集合的条件
- r - 如何在 bookdown 中使用新的 bs4() 主题?
- vba - 如何创建工资/税收计算器?