用户登录
parent
43b1046ae4
commit
ba698c9853
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Auth;
|
||||
|
||||
use App\Constants\Device;
|
||||
use App\Exceptions\BizException;
|
||||
use App\Http\Controllers\Api\V1\Controller;
|
||||
use App\Http\Requests\Api\V1\LoginRequest;
|
||||
use App\Models\User;
|
||||
use App\Services\CaptchaService;
|
||||
|
||||
class LoginController extends Controller
|
||||
{
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param \App\Http\Requests\Api\V1\LoginRequest $request
|
||||
* @param \App\Services\CaptchaService $captchaService
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function __invoke(LoginRequest $request, CaptchaService $captchaService)
|
||||
{
|
||||
$captchaService->validatePhrase(
|
||||
(string) $request->input('captcha_key'),
|
||||
(string) $request->input('captcha_phrase')
|
||||
);
|
||||
|
||||
$user = User::where('phone', $request->input('phone'))->first();
|
||||
|
||||
if (! $user?->verifyPassword($request->input('password'))) {
|
||||
throw new BizException(__('Incorrect account or password'));
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'last_login_at' => now(),
|
||||
'last_login_ip' => $request->realIp(),
|
||||
]);
|
||||
|
||||
$user->tokens()->delete();
|
||||
|
||||
return response()->json(
|
||||
$user->createDeviceToken(Device::UNIAPP)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'phone' => ['bail', 'required', 'string'],
|
||||
'password' => ['bail', 'required', 'string'],
|
||||
'captcha_phrase' => ['bail', 'required', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
":resource not found": ":resource 未找到",
|
||||
"Incorrect account or password": "账户或密码错误",
|
||||
"Invalid captcha": "无效的验证码",
|
||||
"Invalid invitation code": "无效的邀请码",
|
||||
"Invalid verification code": "无效的验证码",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
<?php
|
||||
|
||||
use App\Http\Controllers\Api\V1\Auth\LoginController;
|
||||
use App\Http\Controllers\Api\V1\Auth\RegisterController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::post('login', LoginController::class);
|
||||
Route::post('register', RegisterController::class);
|
||||
|
|
|
|||
Loading…
Reference in New Issue