add token based api auth

This commit is contained in:
TimmensOne 2023-04-03 14:44:33 +02:00
parent ffd98d046e
commit ca9762df9a
2 changed files with 26 additions and 3 deletions

View File

@ -13,7 +13,9 @@ public function up(): void
{ {
Schema::create('personal_access_tokens', function (Blueprint $table) { Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id(); $table->id();
$table->morphs('tokenable'); $table->string('tokenable_type');
$table->string('tokenable_id');
$table->index(["tokenable_type", "tokenable_id"]);
$table->string('name'); $table->string('name');
$table->string('token', 64)->unique(); $table->string('token', 64)->unique();
$table->text('abilities')->nullable(); $table->text('abilities')->nullable();

View File

@ -4,8 +4,11 @@
use App\Models\LocationTransaction; use App\Models\LocationTransaction;
use App\Models\OwnerTransaction; use App\Models\OwnerTransaction;
use App\Models\PurchasingInformation; use App\Models\PurchasingInformation;
use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
/* /*
@ -19,7 +22,25 @@
| |
*/ */
Route::get('/export', function () { Route::post('/login', function (Request $request) {
$fields = $request->validate(
[
'user' => 'required',
'password' => 'required'
]
);
$user = User::where('rz_username', $fields['user'])->first();
if (!$user || !Hash::check($fields['password'], $user->hashed_password)) {
return response([
'message' => 'Bad login'
], 401);
}
$token = $user->createToken('token');
return ['token' => $token->plainTextToken];
});
Route::middleware('auth:sanctum')->get('/export', function () {
return response()->json([ return response()->json([
'Devices' => Device::all(), 'Devices' => Device::all(),
@ -29,7 +50,7 @@
]); ]);
}); });
Route::post('/import', function (Request $request) { Route::middleware('auth:sanctum')->post('/import', function (Request $request) {
$data = $request->json()->all(); $data = $request->json()->all();
$devices = $data['devices']; $devices = $data['devices'];
$purchasingInformations = $data['purchasing_information']; $purchasingInformations = $data['purchasing_information'];