Skip to content

Commit

Permalink
Create ApiKey model
Browse files Browse the repository at this point in the history
  • Loading branch information
range-of-motion committed Oct 26, 2023
1 parent 6647c65 commit 2c5bbc5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
13 changes: 11 additions & 2 deletions app/Http/Controllers/Api/LogInController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\ApiKey;
use App\Models\LoginAttempt;
use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

class LogInController extends Controller
{
Expand All @@ -24,15 +26,22 @@ public function __invoke(Request $request): JsonResponse
'password' => $request->input('password'),
])
) {
$userId = Auth::user()->id;

LoginAttempt::create([
'user_id' => Auth::user()->id,
'user_id' => $userId,
'ip' => $request->ip(),
'failed' => false,
]);

$apiKey = ApiKey::create([
'user_id' => $userId,
'token' => Str::random(32),
]);

return response()
->json([
'token' => 'TOKEN_GOES_HERE',
'token' => $apiKey->token,
]);
} else {
$userByEmail = User::query()
Expand Down
16 changes: 13 additions & 3 deletions app/Http/Controllers/Api/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@

use App\Http\Controllers\Controller;
use App\Http\Resources\TransactionResource;
use App\Models\ApiKey;
use App\Models\Earning;
use App\Models\Spending;
use Illuminate\Http\Request;

class TransactionController extends Controller
{
public function index()
public function index(Request $request)
{
$apiKey = ApiKey::query()
->where('token', $request->header('api-key'))
->first();

if (!$apiKey) {
abort(401);
}

$transactions = collect();

foreach (Earning::all() as $earning) {
foreach (Earning::query()->where('space_id', $apiKey->user->spaces()->first()->id)->get() as $earning) {
$transactions->push($earning);
}

foreach (Spending::all() as $spending) {
foreach (Spending::query()->where('space_id', $apiKey->user->spaces()->first()->id)->get() as $spending) {
$transactions->push($spending);
}

Expand Down
23 changes: 23 additions & 0 deletions app/Models/ApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ApiKey extends Model
{
use HasFactory;

protected $guarded = [];

/**
* Relationships
*/

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
23 changes: 23 additions & 0 deletions database/migrations/2023_10_26_214009_create_api_keys_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('api_keys', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('token')->unique();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('api_keys');
}
};
2 changes: 2 additions & 0 deletions resources/assets/js/prototype/screens/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const logIn = () => {
const json = response.data;
if (json.token) {
localStorage.setItem('api_key', json.token);
router.push('dashboard');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const getTransactionsBySpan = (span) => {
};
const fetchTransactions = () => {
fetch('/api/transactions')
fetch('/api/transactions', { headers: { 'api-key': localStorage.getItem('api_key') } })
.then(response => response.json())
.then(data => {
transactions.value = data;
Expand Down

0 comments on commit 2c5bbc5

Please sign in to comment.