我有一个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,
然后,我将我的自定义提供程序驱动程序添加到提供程序数组的config/auth.php文件中:
'providers' => [
'users' => [
'driver' => 'custom_auth',
],
],
因为我没有使用数据库,所以我去掉了model属性。
最后,我创建了一个名为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是个新手,我能找到的关于我尝试做的事情的教程并不多。任何帮助都会得到极大的重视
发布于 2018-10-19 01:00:54
将您的UserProvider改为使用Illuminate\Contracts\Auth\Authenticatable
而不是App\Authentication\Authenticatable
,如果没有指定,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
}
}
发布于 2018-10-19 06:40:44
您忘记导入Authenticatable
。只需添加:
use Illuminate\Contracts\Auth\Authenticatable;
https://stackoverflow.com/questions/52878467
复制相似问题