【问题标题】:Laravel 5.7 auth can't get successLaravel 5.7 auth 无法成功
【发布时间】:2019-05-21 17:50:47
【问题描述】:

我尝试制作身份验证 API,我已成功注册。 比登录不能成功。 请帮助我,谢谢。

public function login(Request $request)
{
    try
    {
        if (!$request->has('Account') 
        || !$request->has('Password'))
        {
         throw new Exception('Parameter missing');
        }

         $checkUser = DB::table('Users')->where('Account',$request->Account)->first();
        if(empty($checkUser))
        {
            throw new Exception('No Data');
        }
        $data = ([
            'Account' => $request->Account,
            'Password' => $request->Password,
        ]);  

        if(!Auth::attempt($data))
        throw new Exception('Verification error'); 

这个数据库信息。

【问题讨论】:

  • 出示您的注册码。
  • $check = DB::table('Users')->where('Account',$request->Account)->first(); if(!empty($check->UserID)) { throw new Exception('数据重复或存在'); } $user = new User([ 'Account' => $request->Account, 'Password' => bcrypt($request->Password), 'ConfirmPassword' => $request->ConfirmPassword, 'CreateDateTime'=>date ('Ym-d'), 'UpdatedDateTime'=>date('Ym-d') ]); $user->save();
  • 你有没有像'Password' => bcrypt($request->Password),这样对登录的密码进行加密?
  • 错了。每次 ( bcrypt ) 都不一样。
  • 你覆盖了用户名吗?默认情况下,Laravel 使用 email 字段进行身份验证。

标签: php laravel laravel-5 oauth


【解决方案1】:

请尝试以下方法注册需要哈希密码才能保存到数据库中:

User::create([
    'Account' => $request->Account,
    'CreateDateTime' => date('Y-m-d'),
    'UpdatedDateTime' => date('Y-m-d'),
    'Password' => Hash::make($request->Password),
]);  

【讨论】:

  • 我使用 'Password' => Hash::make($request->Password),但我得到了错误。我曾经使用过 Hash。
  • 在顶部添加use Illuminate\Support\Facades\Hash;
【解决方案2】:

尝试这种方式可能会有所帮助,并且还可以使用验证器: 如果不确定密码,那么首先调试

   $table->string('password', 60)->nullable();
    ----------------------------------------------------
   return Validator::make($data, [
        'email' => 'required|email',
        'password' => 'required',
    ]);
    -----------------------------------------------
   $user_data=User::where('username','=',$request->username)->first();
   $userScope=$user_data->scope;

   Input::merge([
        'client_id' => env('CLIENT_ID'),
        'client_secret' => env('CLIENT_SECRET'),
        'scope' => 'admin'
    ]);

    $credentials = $request->only(['grant_type', 'username', 'password','scope']);


    $validationRules = $this->getLoginValidationRules();

    $credentials["client_id"] = env('CLIENT_ID');
    $credentials["client_secret"] = env('CLIENT_SECRET');


    $this->validateOrFail($credentials, $validationRules);

    try {
        if (!$accessToken = Authorizer::issueAccessToken()) {
            return $this->response->errorUnauthorized();
        }
    } catch (\League\OAuth2\Server\Exception\OAuthException $e) {
        throw $e;
        return $this->response->error('could_not_create_token', 500);
    }

    $accessToken["groups"][] = $userScope;

    return response()->json(compact('accessToken'));

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 1970-01-01
    • 2019-05-24
    • 2019-05-22
    • 1970-01-01
    • 2015-12-14
    • 2019-05-13
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多