6
0
Fork 0

优化用户注册

release
李静 2021-11-23 10:40:13 +08:00
parent 9dbcdbd407
commit e12d060bd2
4 changed files with 27 additions and 24 deletions

View File

@ -0,0 +1,9 @@
<?php
namespace App\Constants;
class Device
{
public const PC = 'pc';
public const UNIAPP = 'uniapp';
}

View File

@ -1,18 +0,0 @@
<?php
namespace App\Events\Auth;
use App\Models\User;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class Registered
{
use Dispatchable;
use SerializesModels;
public function __construct(
public User $user
) {
}
}

View File

@ -2,7 +2,7 @@
namespace App\Http\Controllers\Api\V1\Auth;
use App\Events\Auth\Registered;
use App\Constants\Device;
use App\Exceptions\BizException;
use App\Http\Controllers\Api\V1\Controller;
use App\Models\User;
@ -68,10 +68,8 @@ class RegisterController extends Controller
throw new BizException(__('Registration failed, please try again'));
}
Registered::dispatch($user);
return response()->json([
'token' => $user->createToken('app')->plainTextToken,
]);
return response()->json(
$user->createDeviceToken(Device::UNIAPP)
);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Constants\Device;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
@ -94,4 +95,17 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
{
return $this->password && Hash::check($password, $this->password);
}
/**
* 创建设备授权令牌
*
* @param string $device
* @return array
*/
public function createDeviceToken(string $device = null): array
{
return [
'token' => $this->createToken($device ?: Device::PC)->plainTextToken,
];
}
}