【问题标题】:How to change the api authentication model instead of using default user model in laravel如何更改 api 身份验证模型而不是在 laravel 中使用默认用户模型
【发布时间】:2019-11-06 05:59:23
【问题描述】:

我有两个模型客户端和用户。对于网络登录,我使用用户模型作为超级管理员。但我希望通过 api 请求将客户端模型用于移动登录。

api.php

Route::group(['middleware' => 'auth:api'], function() {
    Route::resource('communities', 'communityAPIController');
    Route::resource('communities', 'communityAPIController');
    Route::resource('clients', 'ClientAPIController');
});

配置/auth.php

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

        'api' => [
            'driver' => 'token',
            'provider' => 'clients',
            'hash' => false,
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

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

我有这样的客户端模型。

<?php

namespace App\Models;

use Eloquent as Model;

class Client extends Model
{

    public $table = 'clients';



    public $fillable = [
        'name',
        'phone',
        'house_no',
        'type',
        'is_approved',
        'community_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'name' => 'string',
        'phone' => 'string',
        'house_no' => 'string',
        'type' => 'integer',
        'is_approved' => 'boolean',
        'community_id' => 'integer'
    ];



}

我想为 auth:api 使用客户端模型而不是用户模型。请帮帮我

【问题讨论】:

  • 请出示您的App\Models\Client 逻辑。

标签: laravel api


【解决方案1】:

试试这个



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

        'api' => [
            'driver' => 'token',
            'provider' => 'clients',

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

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

【讨论】:

  • remove \ in 'clients' => [ 'driver' => 'eloquent', 'model' => App\Models\Client::class, 'table'=>'clients' ],
  • 身份验证有效,但是如何获取经过身份验证的客户端模型? @albus_severus
【解决方案2】:
        try This 

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

                'api-clients' => [
                    'driver' => 'token',
                    'provider' => 'clients',
                ],
            ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
            'table' => 'users',
        ],

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



    also make sure to mentions drivers to be used in controller's constructor like this 
    public function __construct()
        {


        auth()->shouldUse('api-clients');


        }

【讨论】:

  • 我应该在 api.php middelware 部分使用什么
  • if (Auth::guard($guard)->check()) { if ('api-clients' === $guard) { } return $next($request); }
  • 我应该把这个放在哪里
猜你喜欢
  • 2018-03-06
  • 1970-01-01
  • 2018-10-29
  • 1970-01-01
  • 2019-08-23
  • 1970-01-01
  • 2017-10-23
  • 2017-04-22
  • 1970-01-01
相关资源
最近更新 更多