通过API进行自定义身份验证

时间:2018-10-18 16:21:06

标签: php laravel authentication oauth-2.0

我有一个laravel API项目。我希望能够发送登录请求并取回令牌,具体取决于一些自定义逻辑。我没有使用数据库,所以无法使用默认身份验证。

我已经建立了一个名为AuthCustomProvider的提供程序。

namespace App\Providers;
use Auth;
use App\Authentication\UserProvider;
use Illuminate\Support\ServiceProvider;

class AuthCustomProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return  void
     */
    public function boot()
    {
        Auth::provider('custom_auth', function($app, array $config) {
            return new UserProvider();
        });
    }
    /**
     * Register bindings in the container.
     *
     * @return  void
     */
    public function register()
    {
        //
    }
}

然后我将其添加到providers数组中的config / app.php文件中:

'providers' => [

    App\Providers\AuthCustomProvider::class,

然后我将我的自定义提供程序驱动程序添加到providers数组中的config / auth.php文件中:

'providers' => [
   'users' => [
       'driver' => 'custom_auth',
   ],
],

因为我不使用数据库,所以我取出了模型属性

最后,我创建了一个名为App / Authentication的文件夹,并将其中的UserProvider.php文件放入其中:

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;

class UserProvider implements IlluminateUserProvider
{
    /**
     * @param    mixed  $identifier
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        // Get and return a user by their unique identifier
    }

    /**
     * @param    mixed   $identifier
     * @param    string  $token
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        // Get and return a user by their unique identifier and "remember me" token
    }

    /**
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    string  $token
     * @return  void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
        // Save the given "remember me" token for the given user
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param    array  $credentials
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        // Get and return a user by looking up the given credentials
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    array  $credentials
     * @return  bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // Check that given credentials belong to the given user
    }

}

最后,我在登录控制器上执行了一个功能。这是api调用的内容:

public function Login(Request $request)
{
        $user  = Consultant::lookup('UserId', 1);
        //Returns collection of user details (user id, username etc)

        //Logic will go here in the future
        $logThemIn = true;

        if ($logThemIn)
        {
            auth()->login($user);
            //return oauth2 token
        }
}

所以这是即时消息,如果我运行此消息,我会收到错误消息:

'App \ Authentication \ UserProvider :: updateRememberToken(App \ Authentication \ Authenticatable $ user,$ token)的声明必须与Illuminate \ Contracts \ Auth \ UserProvider :: updateRememberToken(Illuminate \ Contracts \ Auth \ Authenticatable $ user兼容,$ token)'

我是laravel的新手,我没有很多关于如何尝试做的教程。任何帮助都非常有用

2 个答案:

答案 0 :(得分:0)

将您的UserProvider更改为使用Illuminate\Contracts\Auth\Authenticatable而不是App\Authentication\Authenticatable的UserProvider,如果未指定php,它将从当前名称空间中加载一个类。

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;

class UserProvider implements IlluminateUserProvider
{
    /**
     * @param    mixed  $identifier
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        // Get and return a user by their unique identifier
    }

    /**
     * @param    mixed   $identifier
     * @param    string  $token
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        // Get and return a user by their unique identifier and "remember me" token
    }

    /**
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    string  $token
     * @return  void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
        // Save the given "remember me" token for the given user
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param    array  $credentials
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        // Get and return a user by looking up the given credentials
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    array  $credentials
     * @return  bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // Check that given credentials belong to the given user
    }
}

答案 1 :(得分:0)

您忘记导入Authenticatable。只需添加:

use Illuminate\Contracts\Auth\Authenticatable;