为什么Auth :: attempt在Laravel 5.3中总是返回false? [英] Why is Auth::attempt always returns false in Laravel 5.3?

查看:92
本文介绍了为什么Auth :: attempt在Laravel 5.3中总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel新手.我尝试在Laravel 5.3中使用多重身份验证,而我的auth.php文件是:

I'm new in Laravel. I try to use Multiple Auth in Laravel 5.3 and my auth.php file is:

<?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是:

Then, when I store Clients or Couriers in the DB, I use bcrypt for password (Bring also use the function Hash::make() for passwords). For example, my model Courier is:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Courier extends Authenticatable
{
  [..]

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

  [..]
}

当更新快递时,在我的控制器中,我有:

And when update a courier, in my controller I have:

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).即使如此,请发送有效的凭据.这是我的代码:

I have a method called authenticate but the method attempt always return false (invalid_credentials). Even so send valid credentials.. This is my code:

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'));
    }

我不知道我在做什么错.我在做错什么事吗?

I not know what I'm doing wrong. Anything am I doing wrong?

推荐答案

您已经在模型和控制器上两次加密了密码.

you have encrypt the password twice, on your model and controller.

只需删除其中之一

例如:不要在控制器上使用bcrypt,因为您已经在模型上使用bcrypt.

e.g: don't use bcrypt on your controller, because you have already use bcrypt on your model.

这篇关于为什么Auth :: attempt在Laravel 5.3中总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆