簡體   English   中英

為什么Auth :: attempt在Laravel 5.3中總是返回false?

[英]Why is Auth::attempt always returns false in Laravel 5.3?

我是Laravel新手。 我嘗試在Laravel 5.3中使用多重身份驗證,而我的auth.php文件是:

<?php

return [

    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'courier' => [
            'driver' => 'session',
            'provider' => 'couriers',
        ],

        'client' => [
            'driver' => 'session',
            'provider' => 'clients',
        ]
    ],

    'providers' => [
        'couriers' => [
            'driver' => 'eloquent',
            'model' => App\Courier::class,
        ],

        'clients' => [
            'driver' => 'eloquent',
            'model' => App\Client::class,
        ],

        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ]
    ],

    'passwords' => [
        'couriers' => [
            'provider' => 'couriers',
            'table' => 'password_resets',
            'expire' => 60,
        ],

        'clients' => [
            'provider' => 'clients',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

然后,當我將客戶或快遞員存儲在數據庫中時,我使用bcrypt作為密碼(Bring還使用函數Hash::make()作為密碼)。 例如,我的模型Courier是:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Courier extends Authenticatable
{
  [..]

  public function setPasswordAttribute($pass){
        $this->attributes['password'] = bcrypt($pass);
    }

  [..]
}

當更新快遞時,在我的控制器中,我有:

public function update(Request $request, $id) {

        $fieldsCourier = $request->all();
        $courier = Courier::find($id);

        if( isset($fieldsCourier['password']) )
            $fieldsCourier['password'] = bcrypt($fieldsCourier['password']);

        if( $courier->update($fieldsCourier) )
            $courier = Courier::find($id);

    }

我有一個稱為Authenticate的方法,但是該方法嘗試始終返回false(invalid_credentials)。 即使這樣發送有效的憑據。這是我的代碼:

public function authenticate(Request $request) {
        $credentials = $request->only('email', 'password');

        try {
            if ( auth()->guard('courier')->attempt($credentials) ){
                $user = Auth::guard('courier')->user();
            } else {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        return response()->json(compact('user'));
    }

我不知道我在做什么錯。 我做錯什么了嗎?

您已經在模型和控制器上兩次對密碼進行了加密。

只需刪除其中之一

例如:不要在控制器上使用bcrypt ,因為您已經在模型上使用bcrypt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM