我正在使用laravel passport和axios,我想检查经过身份验证的用户的当前访问令牌是否真的在我的数据库中,所以我做了什么
Auth.js。在这个函数上,我不仅想检查localStorage中是否有令牌,还要检查它是否真的在数据库中。
isAuthenticated(){
if(this.getToken()){
//get access_token
axios({
method: 'get',
url: '/prod/api/authcheck',
data: {},
config : 'JSON'
})
.then(response=>{
})
.catch(response=>{
});
return true
}else{
return false
}
}
路线
Route::get('/authcheck', 'HelperController@checkAuth')->name('authcheck');
HelperController.php。
public function checkAuth(Request $request){
$user_id = $request->user();
print_r($request->user());
}
在这一部分,当我尝试print_r($request->user())
时,它给了我这个输出
App\User Object
(
[fillable:protected] => Array
(
[0] => name
[1] => email
[2] => password
)
[hidden:protected] => Array
(
[0] => password
[1] => remember_token
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[name] => biansor
[email] => bs@gmail.com
[password] => $2y$10$KPCD4Vbgqqaxxcqa8bNpQ.UDBfkETO4Krf9dFEMKd4.uN8c0qTD52
[remember_token] => T8nVWkJMaO6fr51rqu6HWzdtUri3c9nK9bdsSqSTSBQ7xD1qXNN1taPQWBlZ
[created_at] =>
[updated_at] => 2017-11-19 15:31:24
)
[original:protected] => Array
(
[id] => 1
[name] => biansor
[email] => bs@gmail.com
[password] => $2y$10$KPCD4Vbgqqaxxcqa8bNpQ.UDBfkETO4Krf9dFEMKd4.uN8c0qTD52
[remember_token] => T8nVWkJMaO6fr51rqu6HWzdtUri3c9nK9bdsSqSTSBQ7xD1qXNN1taPQWBlZ
[created_at] =>
[updated_at] => 2017-11-19 15:31:24
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[rememberTokenName:protected] => remember_token
[accessToken:protected] => Laravel\Passport\Token Object
(
[table:protected] => oauth_access_tokens
[incrementing] =>
[guarded:protected] => Array
(
)
[casts:protected] => Array
(
[scopes] => array
[revoked] => bool
)
[dates:protected] => Array
(
[0] => expires_at
)
[timestamps] =>
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => bd38c210ae36780ff4165f7ae468d156c9ebb24c2c5fe30c5ac40c49c1a2bc4d9396b9518934244b
[user_id] => 1
[client_id] => 2
[name] =>
[scopes] => []
[revoked] => 0
[created_at] => 2017-11-21 14:10:22
[updated_at] => 2017-11-21 14:10:22
[expires_at] => 2018-11-21 14:10:22
)
[original:protected] => Array
(
[id] => bd38c210ae36780ff4165f7ae468d156c9ebb24c2c5fe30c5ac40c49c1a2bc4d9396b9518934244b
[user_id] => 1
[client_id] => 2
[name] =>
[scopes] => []
[revoked] => 0
[created_at] => 2017-11-21 14:10:22
[updated_at] => 2017-11-21 14:10:22
[expires_at] => 2018-11-21 14:10:22
)
[changes:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
)
所以现在这就是我需要的,我怎样才能找回这部分中的任何一个?这样我就可以获得access_token [id]
[attributes:protected] => Array
(
[id] => bd38c210ae36780ff4165f7ae468d156c9ebb24c2c5fe30c5ac40c49c1a2bc4d9396b9518934244b
[user_id] => 1
[client_id] => 2
[name] =>
[scopes] => []
[revoked] => 0
[created_at] => 2017-11-21 14:10:22
[updated_at] => 2017-11-21 14:10:22
[expires_at] => 2018-11-21 14:10:22
)
[original:protected] => Array
(
[id] => bd38c210ae36780ff4165f7ae468d156c9ebb24c2c5fe30c5ac40c49c1a2bc4d9396b9518934244b
[user_id] => 1
[client_id] => 2
[name] =>
[scopes] => []
[revoked] => 0
[created_at] => 2017-11-21 14:10:22
[updated_at] => 2017-11-21 14:10:22
[expires_at] => 2018-11-21 14:10:22
)
如果我只是返回response()->json($request->user())
,它只会给我一个用户created_at,email,id,name,updated_at的表格。
AccessToken模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class AccessToken extends Model
{
protected $table = 'oauth_access_token';
public function refresh_token(){
return $this->hasMany('App\RefreshToken', 'id', 'access_token_id');
}
public function users(){
return $this->belongsTo('App\User', 'id', 'user_id');
}
}