mirror of
https://github.com/JGH0/Todo-App-Backend.git
synced 2026-06-03 13:28:47 +02:00
added API and login
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateApiAuthKeysTable extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'CHAR',
|
||||
'constraint' => 36,
|
||||
'null' => false,
|
||||
],
|
||||
'user_id' => [
|
||||
'type' => 'CHAR',
|
||||
'constraint' => 36,
|
||||
'null' => false,
|
||||
],
|
||||
'key_hash' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
'null' => false,
|
||||
'comment' => 'SHA-256 hash of the API key',
|
||||
],
|
||||
'key_prefix' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 20,
|
||||
'null' => false,
|
||||
'comment' => 'First 8 characters for identification',
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
'null' => true,
|
||||
'comment' => 'User-friendly name for the key',
|
||||
],
|
||||
'scopes' => [
|
||||
'type' => 'JSON',
|
||||
'null' => true,
|
||||
'comment' => 'Array of allowed scopes (e.g., ["read", "write"])',
|
||||
],
|
||||
'expires_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
'comment' => 'Optional expiration date',
|
||||
],
|
||||
'last_used_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
'last_used_ip' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 45,
|
||||
'null' => true,
|
||||
'comment' => 'IPv4 or IPv6 address',
|
||||
],
|
||||
'is_active' => [
|
||||
'type' => 'BOOLEAN',
|
||||
'default' => true,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('user_id');
|
||||
$this->forge->addKey('key_hash');
|
||||
$this->forge->addKey('is_active');
|
||||
$this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
|
||||
$this->forge->createTable('api_auth_keys');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('api_auth_keys');
|
||||
}
|
||||
}
|
||||
@@ -309,5 +309,38 @@ class SampleDataSeeder extends Seeder
|
||||
if (!empty($recurringTaskCategories)) {
|
||||
$this->db->table('recurring_task_categories')->insertBatch($recurringTaskCategories);
|
||||
}
|
||||
|
||||
// Create an API key for the demo user
|
||||
$existingApiKey = $this->db->table('api_auth_keys')
|
||||
->where('user_id', $userId)
|
||||
->where('name', 'Demo API Key')
|
||||
->get()
|
||||
->getRowArray();
|
||||
|
||||
if (!$existingApiKey) {
|
||||
$apiKey = 'todo_' . bin2hex(random_bytes(32));
|
||||
$keyHash = hash('sha256', $apiKey);
|
||||
$keyPrefix = substr($apiKey, 0, 8);
|
||||
|
||||
$this->db->table('api_auth_keys')->insert([
|
||||
'id' => $generateUuid(),
|
||||
'user_id' => $userId,
|
||||
'key_hash' => $keyHash,
|
||||
'key_prefix' => $keyPrefix,
|
||||
'name' => 'Demo API Key',
|
||||
'scopes' => json_encode(['read', 'write']),
|
||||
'expires_at' => null,
|
||||
'is_active' => true,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
echo "\n========================================\n";
|
||||
echo "DEMO API KEY CREATED:\n";
|
||||
echo "========================================\n";
|
||||
echo "API Key: {$apiKey}\n";
|
||||
echo "Prefix: {$keyPrefix}\n";
|
||||
echo "Use this key in the X-API-Key header\n";
|
||||
echo "========================================\n\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user