Skip to content

Commit

Permalink
feature: Access token page
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Sep 25, 2023
1 parent f6d5127 commit 36ba833
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
64 changes: 64 additions & 0 deletions app/Http/Controllers/API/Settings/AccessTokenController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Http\Controllers\API\Settings;

use App\Http\Controllers\Controller;
use App\Models\AccessToken;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class AccessTokenController extends Controller
{
/**
* List users access tokens
*
* @return JsonResponse
*/
public function index()
{
return auth('api')->user()
->accessTokens()
->get();
}

/**
* Create access tokens
*
* @return JsonResponse
*/
public function create(Request $request)
{
$token = Str::uuid();
AccessToken::create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'token' => $token,
'ip_range' => $request->ip_range,
]);

return response()->json([
'token' => $token
]);
}

/**
* Revoke access tokens
*
* @return JsonResponse
*/
public function delete(Request $request)
{
$token = AccessToken::find($request->token_id);
if (! $token || $token->user_id != user()->id) {
return response()->json([
'message' => 'Anahtar bulunamadı.'
], 404);
}
$token->delete();

return response()->json([
'message' => 'Anahtar başarıyla silindi.'
]);
}
}
2 changes: 2 additions & 0 deletions app/Models/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ class AccessToken extends Model
'last_used_ip',
'ip_range',
];

protected $hidden = ["token"];
}
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@
Route::delete('/', [Settings\VaultController::class, 'delete']);
});

// Personal Access Tokens
Route::group(['prefix' => 'settings/tokens'], function () {
Route::get('/', [Settings\AccessTokenController::class, 'index']);
Route::post('/', [Settings\AccessTokenController::class, 'create']);
Route::delete('/{token_id}', [Settings\AccessTokenController::class, 'delete']);
});

// Settings
Route::group(['prefix' => 'settings', 'middleware' => ['admin']], function () {
// Extension
Expand Down

0 comments on commit 36ba833

Please sign in to comment.