首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过API进行自定义身份验证

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

Stack Overflow用户
提问于 2018-10-19 00:21:06
回答 2查看 327关注 0票数 0

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

我已经设置了一个名为AuthCustomProvider的提供者。

代码语言:javascript
复制
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文件中:

代码语言:javascript
复制
'providers' => [

    App\Providers\AuthCustomProvider::class,

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

代码语言:javascript
复制
'providers' => [
   'users' => [
       'driver' => 'custom_auth',
   ],
],

因为我没有使用数据库,所以我去掉了model属性。

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

代码语言:javascript
复制
<?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调用将转到以下位置:

代码语言:javascript
复制
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是个新手,我能找到的关于我尝试做的事情的教程并不多。任何帮助都会得到极大的重视

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-19 01:00:54

将您的UserProvider改为使用Illuminate\Contracts\Auth\Authenticatable而不是App\Authentication\Authenticatable,如果没有指定,php将从当前名称空间装入一个类。

代码语言:javascript
复制
<?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
    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-10-19 06:40:44

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

代码语言:javascript
复制
use Illuminate\Contracts\Auth\Authenticatable;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52878467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档