✨ token 生成接口
This commit is contained in:
@@ -55,4 +55,9 @@ class ImageController extends Controller
|
||||
'key', 'name', 'extension', 'pathname', 'origin_name', 'size', 'mimetype', 'md5', 'sha1', 'links'
|
||||
));
|
||||
}
|
||||
|
||||
public function images(Request $request): Response
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,65 +5,37 @@ namespace App\Http\Controllers\Api\V1;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class TokenController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
public function store(Request $request): Response
|
||||
{
|
||||
//
|
||||
try {
|
||||
$request->validate([
|
||||
'email' => 'required|email',
|
||||
'password' => 'required',
|
||||
]);
|
||||
} catch (ValidationException $e) {
|
||||
return $this->error($e->validator->errors()->first());
|
||||
}
|
||||
|
||||
/** @var User|null $user */
|
||||
$user = User::query()->where('email', $request->email)->first();
|
||||
|
||||
if (! $user || ! Hash::check($request->password, $user->password)) {
|
||||
return $this->error('The email address or password is incorrect.');
|
||||
}
|
||||
|
||||
$token = $user->createToken($user->email)->plainTextToken;
|
||||
|
||||
return $this->success('success', compact('token'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function clear(): Response
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $request->user();
|
||||
$token = $user->createToken('test');
|
||||
|
||||
return $this->success('success', $token->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,84 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-xl mb-2 text-gray-800 font-semibold">授权相关</p>
|
||||
<div class="space-y-4 bg-gray-50 p-3 rounded-md mb-5">
|
||||
<div>
|
||||
<p class="text-lg text-gray-700 font-semibold">生成 Token</p>
|
||||
<x-code><span class="text-green-500 select-none">POST </span>/tokens</x-code>
|
||||
<div class="my-4 overflow-x-auto">
|
||||
<p class="text-sm mb-2">请求参数</p>
|
||||
<table class="min-w-full">
|
||||
<thead class="bg-gray-50 border">
|
||||
<tr>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
字段
|
||||
</th>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
类型
|
||||
</th>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
说明
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="bg-white border divide-y text-sm">
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">email</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">String</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">邮箱</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">password</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">String</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">密码</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="my-4 overflow-x-auto">
|
||||
<p class="text-sm mb-2">返回参数</p>
|
||||
<table class="min-w-full">
|
||||
<thead class="bg-gray-50 border">
|
||||
<tr>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
字段
|
||||
</th>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
类型
|
||||
</th>
|
||||
<th scope="col" class="px-3 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider whitespace-nowrap">
|
||||
说明
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="bg-white border divide-y text-sm">
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">status</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">Boolean</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">状态,true 或 false</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">message</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">String</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">描述信息</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">data</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">Object</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">数据</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap pl-6">token</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">String</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">Token</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-xl mb-2 text-gray-800 font-semibold">图片相关</p>
|
||||
<div class="space-y-4 bg-gray-50 p-3 rounded-md mb-5">
|
||||
<div>
|
||||
@@ -140,7 +218,7 @@
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap">data</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">Object</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">图片数据</td>
|
||||
<td class="px-3 py-2 whitespace-nowrap">数据</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="px-3 py-2 whitespace-nowrap pl-6">key</td>
|
||||
|
||||
+2
-1
@@ -17,10 +17,11 @@ use App\Http\Controllers\Api\V1\TokenController;
|
||||
|
||||
Route::group(['prefix' => 'v1'], function () {
|
||||
Route::post('upload', [ImageController::class, 'upload']);
|
||||
Route::post('tokens', [TokenController::class, 'store'])->middleware('throttle:3,1');
|
||||
|
||||
Route::group([
|
||||
'middleware' => 'auth:sanctum',
|
||||
], function () {
|
||||
Route::resource('tokens', TokenController::class);
|
||||
Route::delete('tokens/clear', [TokenController::class, 'clear']);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user