如何更改令牌卫士中的api_token列 [英] how to change api_token column in token guard

查看:109
本文介绍了如何更改令牌卫士中的api_token列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5.5

我想更改 TokenGaurd 中使用的api令牌的方向,因此, 我创建了一个名为 CafeTokenGaurd 的自定义防护,它扩展了TokenGuard,并根据需要定义了__construct函数,如下所示:

I want to change direction of api token that used in TokenGaurd so, i created a custom guard named CafeTokenGaurd extends TokenGuard, i define __construct function into it like what i want, something like this:

public function __construct(UserProvider $provider, Request $request) {
        parent::__construct($provider, $request);
        $this->inputKey = 'api_key'; // I want changing this part
        $this->storageKey = 'api_key';
    }

现在,我想从与用户表的关系中定义api_key,如下所示:

Now i want to define api_key from relation with users table like this:

device_user table -> token

我想为用户拥有的每个设备定义特定的令牌,并且我要在用户和设备之间的数据透视表中将此列的api键输入和存储键设置为

i want to define specific tokens for each devices user have, and i want to set api key input and storage key to this column in pivot table between users and devices,

我该怎么办?!

谢谢

推荐答案

由于您需要更改从数据库中检索用户的方式,因此实际上您需要创建并使用自定义UserProvider而不是自定义.仅当您想从api_token重命名输入键或存储键时,才需要自定义保护.

Because you need to change how the user is retrieved out of the database, you actually need to create and use a custom UserProvider, not a custom Guard. You'll only need the custom guard if you feel like renaming the input key or storage key from api_token.

因此,您将需要一个新的自定义UserProvider类,该类知道如何使用给定的凭据(令牌)来检索您的用户,并且需要告诉Auth使用新的自定义UserProvider

So, you'll need a new custom UserProvider class that knows how to retrieve your user with the given credentials (token), and you'll need to tell Auth to use your new custom UserProvider class.

首先,假设您仍在使用Eloquent,请先创建一个新的UserProvider类,该类扩展了基础EloquentUserProvider类.在此示例中,它是在app/Services/Auth/MyEloquentUserProvider.php下创建的.在此类中,您将需要使用有关如何使用提供的令牌检索用户的详细信息来覆盖retrieveByCredentials函数.

First, assuming you're still using Eloquent, start by creating a new UserProvider class that extends the base EloquentUserProvider class. In this example, it is created at app/Services/Auth/MyEloquentUserProvider.php. In this class, you will need to override the retrieveByCredentials function with the details on how to retrieve the user with the provided token.

namespace App\Services\Auth;

use Illuminate\Auth\EloquentUserProvider;

class MyEloquentUserProvider extends EloquentUserProvider
{
    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        if (empty($credentials)) {
            return;
        }

        // $credentials will be an array that looks like:
        // [
        //     'api_token' => 'token-value',
        // ]

        // $this->createModel() will give you a new instance of the class
        // defined as the model in the auth config for your application.

        // Your logic to find the user with the given token goes here.

        // Return found user or null if not found.
    }
}

一旦创建了类,就需要让Auth知道它.您可以在AuthServiceProvider服务提供商上的boot()方法中执行此操作.此示例将使用名称"myeloquent",但您可以使用任何名称("eloquent"和"database"除外).

Once you've created your class, you need to let Auth know about it. You can do this in the boot() method on your AuthServiceProvider service provider. This example will use the name "myeloquent", but you can use whatever you want (except "eloquent" and "database").

public function boot()
{
    $this->registerPolicies();

    Auth::provider('myeloquent', function($app, array $config) {
        return new \App\Services\Auth\MyEloquentUserProvider($app['hash'], $config['model']);
    });
}

最后,您需要告诉Auth使用新的myeloquent用户提供程序.这是在config/auth.php配置文件中完成的.

And finally, you need to tell Auth to use your new myeloquent user provider. This is done in the config/auth.php config file.

'providers' => [
    'users' => [
        'driver' => 'myeloquent', // this is the provider name defined above
        'model' => App\User::class,
    ],
],

您可以在此处的文档中阅读有关添加自定义用户提供程序的更多信息.

这篇关于如何更改令牌卫士中的api_token列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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