laravel使用jwt做小程序认证,不使用默认的password字段
我在做小程序登录状态认证,小程序登录前端已经发送code
给我,向微信认证过了,所以我觉得已经不需要password
了。
我用的是tymon/jwt-auth,好像默认必须要使用password
做验证,如果只是改名的话,就简单了,可去掉password
这个字段的话,貌似只能在源码里做改动了
各位老哥,有办法在不改动源码的情况下 ,去掉password
字段做小程序登录验证吗?
对了,我还用了多表验证隔离,现在代码是这样的
user.php
<?php
namespace App\Model;
/**
* 店铺模型,店铺登录模型
*/
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class Shop extends Authenticatable implements JWTSubject
{
use Notifiable;
/**
* 表名称
* @var string
*/
protected $table = 'shop';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
// 'username', 'email', 'password',
'username' , 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* 获取会储存到 jwt 声明中的标识
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* 返回包含要添加到 jwt 声明中的自定义键值对数组
* @return array
*/
public function getJWTCustomClaims()
{
return ['role' => 'shopadmin'];
}
}
login.php
<?php
namespace App\Http\Controllers\ShopAdmin;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;
class LoginController extends Controller
{
function __construct( )
{
$this->middleware('auth:shopadmin', ['except' => ['login']]);
}
public function login()
{
$credentials = request(['username', 'password']);
$token = auth('shopadmin')->attempt($credentials);
if ( !$token ) {
return $this->fail( 50003 , '账号或者密码错误!' );
}
return $this->success( $this->respondWithToken($token) );
}
public function me()
{
return response()->json(auth('shopadmin')->user());
}
public function logout()
{
auth('shopadmin')->logout();
return $this->success( '成功退出登录!Successfully logged out!' );
}
public function refresh()
{
$data = $this->respondWithToken(auth('shopadmin')->refresh());
return $this->success( $data );
}
/**
* @param $token
* @return JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('shopadmin')->factory()->getTTL() * 1
]);
}
}
解决了,按文档说的,返回用户实例,为了省事我查询 使用了db,粗心了,我以为只要是数据库查询结果 就可以了,当我改成使用集成jwt模型,并且完成文档所说的契约方法后,就可以了,附代码