问题描述
我有lumen + jwt restapi,带有自定义用户表(例如:pengguna),其中nomor作为主键,tgl_lahir作为密码.. api / login没有问题,它会生成令牌,但是当我尝试使用其他路由时,例如作为api / buku,尽管授权标头在登录后包含有效令牌,但返回总是401未经授权
我的模特喜欢
<?PHP
namespace App;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Model implements AuthenticatableContract,AuthorizableContract,JWTSubject
{
use Authenticatable,Authorizable;
protected $primaryKey = 'nomor';
protected $table = 'pengguna';
public $timestamps = false;
protected $fillable = [
'nomor','nama','alamat'
];
protected $hidden = [
'tgl_lahir ',];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}
我的BukuController
<?PHP
namespace App\Http\Controllers;
use App\Buku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BukuController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function showAllBuku()
{
return response()->json(Buku::all());
}
}
我的路线
$router->group(['prefix' => 'api'],function () use ($router) {
$router->post('login','AuthController@login');
$router->get('buku',['uses' => 'BukuController@showAllBuku']);
});
<?PHP
return [
'defaults' => [
'guard' => 'api','passwords' => 'users',],'guards' => [
'api' => [
'driver' => 'jwt','provider' => 'users','providers' => [
'users' => [
'driver' => 'eloquent','model' => \App\User::class
]
],];
如果我在Middleware \ Authenticate中注释了代码,则现有的pengguna表不允许创建像lumen / laravel auth这样的ID字段:
public function handle($request,Closure $next,$guard = null)
{
//if this block commented is working
if ($this->auth->guard($guard)->guest()) {
return response('Unauthorized.',401);
}
return $next($request);
}
它正在工作..我的案件还有别的方法吗?谢谢您的帮助
解决方法
对不起,我的错误,我的问题通过在用户模型中添加此问题而解决
public function getAuthIdentifierName(){
return $this->nomor;
}
public function getAuthIdentifier(){
return $this->{$this->getAuthIdentifierName()};
}