我设法创建了jwtauth将移动应用程序连接到octobercms后端 来自这个reference 但是last_login字段始终为空,我相信默认情况下未设置。 这是我拥有的经过身份验证的功能
use Tymon\JWTAuth\JWTAuth;
public function __construct(JWTAuth $auth)
{
$this->auth = $auth;
}
public function authenticate(Request $request)
{
try {
if (! $token = $this->auth->attempt($request->only('email', 'password'))) {
return response()->json([
'errors' => [
'root' => 'Could not sign you in with those details.',
],
], 401);
}
} catch (JWTException $e) {
return response()->json([
'errors' => [
'root' => 'Failed.',
],
], $e->getStatusCode());
}
return response()->json([
'data' => $request->user(),
'meta' => [
'token' => $token,
],
]);
}
由jwtauth文件夹中的route.php调用
Route::group(['prefix' => 'api'], function () {
Route::post('auth/login','Autumn\JWTAuth\Http\Controllers\AuthController@authenticate');
Route::post('auth/register', 'Autumn\JWTAuth\Http\Controllers\AuthController@register');
Route::post('auth/logout', 'Autumn\JWTAuth\Http\Controllers\AuthController@logout');
Route::group(['middleware' => 'jwt.auth'], function () {
Route::get('auth/me', 'Autumn\JWTAuth\Http\Controllers\AuthController@user');
});
我们如何设置用户的last_login时间戳? 希望我的问题清楚易懂。
添加了plugin.php,因为我在实施他的建议时出现异常,因此我按照@HardikSatasiya的要求扩展了用户插件
use System\Classes\PluginBase;
use Rainlab\User\Controllers\Users as UsersController;
use Rainlab\User\Models\User as UserModels;
use Event;
class Plugin extends PluginBase
{
public function registerComponents()
{
}
public function registerSettings()
{
}
public function boot()
{
UserModels::extend(function($model){
$model->bindEvent('model.beforeSave',function() use ($model) {
$users = \BackendAuth::getUser();
$model->backend_users_id = $users->id;
//above line result exception when calling method as @HardikSatasiya suggested
if(!empty($model->avatar)){
$model->image_path = $model->avatar->getPath();
}
if(!empty($model->groups)){
$model->membership = $model->groups[0]['name'];
}
});
$model->addJsonable('users_detail','membership');
});
UsersController::extendFormFields(function($form,$model,$context){
$form->addTabFields([
'users_detail[0][gender]' => [
'label' => 'Jenis Kelamin',
'span' => 'left',
'tab' => 'User Profile',
'type' => 'radio',
'options' => [
'Pria' => 'Pria',
'Wanita' => 'Wanita'
]
],
'users_detail[0][ttl]' => [
'label' => 'Tempat/Tanggal Lahir',
'type' => 'text',
'span' => 'left',
'tab' => 'User Profile'
],
]);
});
}
我通过这个单独的插件向用户表添加了其他字段。
答案 0 :(得分:1)
好吧,可能是因为此插件从外部登录用户时未调用内部挂钩。
可能是我们需要手动调用,此代码段可以做到这一点,只需在成功登录后输入
code
。
// this will fire hooks and update `last_login`
// get authenticated user from the jwt authmanager
$user = $this->auth->authenticate($token);
$user->afterLogin();
在下面的代码中添加了
。
public function authenticate(Request $request)
{
try {
if (! $token = $this->auth->attempt($request->only('email', 'password'))) {
return response()->json([
'errors' => [
'root' => 'Could not sign you in with those details.',
],
], 401);
}
} catch (JWTException $e) {
return response()->json([
'errors' => [
'root' => 'Failed.',
],
], $e->getStatusCode());
}
// this will fire hooks and update `last_login`
// get authenticated user from the jwt authmanager
$user = $this->auth->authenticate($token);
$user->afterLogin();
// ^ this code
return response()->json([
'data' => $request->user(),
'meta' => [
'token' => $token,
],
]);
}
此代码段将按预期更新
last_login
。我没有测试它,但是它可以正常工作。
如有任何疑问或问题,请发表评论。