2

我正在为移动应用程序创建一个 Laravel (5.7) 应用程序。所以我有一个 API 和一个 Web 面板,都需要登录并且每个都有一个模型。对于 Web 登录,我使用User模型(因为这是操作角色)和Client通过应用程序注册的用户的另一个模型。

我正在使用JWT为移动应用程序创建身份验证令牌并使用 Web 面板的常规登录。

复杂之处在于默认的 auth.phpguardweb,如果我使用 API 中的(以下)身份验证方法,它会查看用户表,而不是客户端表,并且当我将默认保护更改为 api 时它已修复但Web 登录然后尝试查看clients

因此,简而言之,我尝试以多种不同的方式切换此默认防护,但它不起作用。我做过的一些测试(失败的)是:

  • 将登录控制器中的 $guard 变量更改为 web 并在 auth.php 中将 api 设置为默认值
  • Config::set('auth.defaults.guard' , 'api');在我的 API 的身份验证方法中使用or config('auth.defaults.guard' , 'api');(及其所有变体)在执行时覆盖默认的 auth.php 守卫

这是我的 auth.php 文件

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

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

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

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

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

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

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

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

];

我的 ApiClientController.php 中的身份验证方法

 public function authenticate(Request $request)
    {
//        config('auth.defaults.guard' , 'api'); // NOT WORKING!!
//        Config::set('auth.guards.web.provider', 'clients'); // NOT WORKING!!
//        Config::set('auth.providers.users.model', Client::class); // NOT WORKING!!
//        config('auth.providers.users.model', Client::class); // NOT WORKING!!

        $credentials = $request->only('phone', 'password');

        try {
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 400);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        Log::info("JWT Token: $token");

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

另外,这是我的客户模型

<?php

namespace App;


use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Client extends Authenticatable implements JWTSubject
{

    protected $hidden = [
        'password', 'phone_verification_code', 'phone_verified_at'
    ];

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }

}
4

1 回答 1

0

$request实例有一个user接受一个参数的方法:

$request->user('apiguard');

如果您正在尝试身份验证:

Auth::guard('apiguard')->attempt($credentials);
于 2018-11-20T18:25:37.017 回答