diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index f6cbf215..b345c375 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -5,7 +5,7 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; - +use Illuminate\Support\Facades\Log; class LoginController extends Controller { @@ -16,6 +16,21 @@ public function index() public function store(Request $request) { - return redirect()->route('home'); + // Validate the request + $request->validate([ + 'email' => 'required|email', + 'password' => 'required', + ]); + + // Attempt to log the user in + if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) { + return redirect()->intended(route('home'))->with('message', 'You are logged in!'); + } + Log::info('Available guards', ['guards' => config('auth.guards')]); + Log::warning('Failed login attempt', ['email' => $request->email]); + + return back()->withErrors([ + 'email' => 'The provided credentials do not match our records.', + ])->withInput($request->only('email')); } } diff --git a/app/Http/Controllers/Auth/LogoutController.php b/app/Http/Controllers/Auth/LogoutController.php index f59bdb38..9f2e2ef9 100644 --- a/app/Http/Controllers/Auth/LogoutController.php +++ b/app/Http/Controllers/Auth/LogoutController.php @@ -3,14 +3,23 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class LogoutController extends Controller { - public function logout() + public function logout(Request $request) { + // Logout the user Auth::logout(); - return redirect()->route('home'); + // Invalidate the session + $request->session()->invalidate(); + + // Regenerate CSRF token + $request->session()->regenerateToken(); + + // Redirect to the home page + return redirect()->route('home')->with('message', 'You have been logged out successfully.'); } } diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 09c47c29..40c8af41 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -16,26 +16,29 @@ public function index() } public function store(Request $request) - { - //validate - $this->validate($request, [ - 'username' => 'required|max:255', - 'email' => 'required|email|max:255', - 'password' => 'required|confirmed' - ]); +{ + // Validate the request + $this->validate($request, [ + 'username' => 'required|max:255|unique:users', // Ensure username is unique + 'email' => 'required|email|max:255|unique:users', // Ensure email is unique + 'password' => 'required|string|min:8|confirmed', // Password must match confirmation + ]); - //store user - User::create([ - 'username' => $request->username, - 'email' => $request->email, - 'password' => Hash::make($request->password) - ]); + // Create and store the user with default values + $user = User::create([ + 'username' => $request->username, + 'email' => $request->email, + 'password' => Hash::make($request->password), // Hash the password + 'xp' => 0, // Starting XP + 'rank' => 'Quiz Apprentice', // Default rank for new users based on initial XP + 'total_correct_answers' => 0, // No correct answers at start + 'total_questions_answered' => 0, // No questions answered at start + 'percentage_correct' => 0, // No percentage data at start + ]); - //sign user in - $credentials = $request->only('email', 'password'); - Auth::attempt($credentials); + // Automatically log in the user after registration + Auth::login($user); - //redirect - return redirect()->route('home'); - } + return redirect()->route('profile'); +} } diff --git a/app/Http/Controllers/LeaderboardController.php b/app/Http/Controllers/LeaderboardController.php index e6c25c91..4dd367bc 100644 --- a/app/Http/Controllers/LeaderboardController.php +++ b/app/Http/Controllers/LeaderboardController.php @@ -2,12 +2,24 @@ namespace App\Http\Controllers; +use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class LeaderboardController extends Controller { public function index() { - return view('leaderboard',); + // Fetch the top 10 users ordered by XP, including the sum of correct answers from quizzes + $users = User::with('quizzes') // Eager load the quizzes + ->select('id', 'username', 'xp') + ->withCount(['quizzes as total_correct_answers' => function ($query) { + $query->select(DB::raw('COALESCE(SUM(correct_answers), 0)')); + }]) + ->orderBy('xp', 'desc') + ->take(10) + ->get(); + + return view('leaderboard', compact('users')); } } diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 953882f3..19106bc2 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -21,4 +21,24 @@ public function index() return view('profile', ['art' => $art, 'geography' => $geography, 'history' => $history, 'science' => $science, 'sports' => $sports]); } + public function show() + { + $user = Auth::user(); // Fetch the logged-in user's data + + // Calculate the user's rank based on XP + if ($user->xp >= 10000) { + $rank = 'Quiz Master'; + } elseif ($user->xp >= 5000) { + $rank = 'Epic Quizer'; + } elseif ($user->xp >= 1500) { + $rank = 'Average Quizer'; + } else { + $rank = 'Quiz Apprentice'; + } + + return view('profile', [ + 'user' => $user, + 'rank' => $rank + ]); + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 30020a50..c0b00d6b 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -33,10 +33,12 @@ class Kernel extends HttpKernel \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, - // \Illuminate\Session\Middleware\AuthenticateSession::class, + \Illuminate\Session\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, + // \App\Http\Middleware\Authenticate::class + // 'checkLoggedIn' => \App\Http\Middleware\CheckUserLoggedIn::class, ], 'api' => [ diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 704089a7..64701746 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -3,6 +3,7 @@ namespace App\Http\Middleware; use Illuminate\Auth\Middleware\Authenticate as Middleware; +use Illuminate\Support\Facades\Log; class Authenticate extends Middleware { @@ -15,6 +16,7 @@ class Authenticate extends Middleware protected function redirectTo($request) { if (! $request->expectsJson()) { + Log::info('Redirecting to login - Not authenticated'); return route('login'); } } diff --git a/app/Http/Middleware/CheckUserLoggedIn.php b/app/Http/Middleware/CheckUserLoggedIn.php new file mode 100644 index 00000000..9abaf6ab --- /dev/null +++ b/app/Http/Middleware/CheckUserLoggedIn.php @@ -0,0 +1,24 @@ +route('login'); // Redirect to the login page if not authenticated + } + + return $next($request); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 63edf53b..15be8812 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -23,7 +23,18 @@ class User extends Authenticatable 'xp', 'rank' ]; - + public function getRankAttribute() + { + if ($this->xp < 1500) { + return 'Quiz Apprentice'; + } elseif ($this->xp >= 1500 && $this->xp < 5000) { + return 'Average Quizer'; + } elseif ($this->xp >= 5000 && $this->xp < 10000) { + return 'Epic Quizer'; + } else { + return 'Quiz Master'; + } + } public function leaderboard() { $topPlayers = User::orderBy('xp', 'desc')->take(10)->get(); @@ -49,8 +60,13 @@ public function leaderboard() 'email_verified_at' => 'datetime', ]; - public function quizes() + public function quizzes() { return $this->hasMany(Quiz::class); } + + public function categoryScores() + { + return $this->hasMany(CategoryScore::class); + } } diff --git a/config/auth.php b/config/auth.php index ba1a4d8c..53b71cb2 100644 --- a/config/auth.php +++ b/config/auth.php @@ -14,7 +14,7 @@ */ 'defaults' => [ - 'guard' => 'web', + 'guard' => 'web', 'passwords' => 'users', ], @@ -35,19 +35,17 @@ | */ - 'guards' => [ + 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], - 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ], - /* |-------------------------------------------------------------------------- | User Providers diff --git a/database/migrations/2024_09_25_152710_create_categories_table.php b/database/migrations/2020_09_25_152710_create_categories_table.php similarity index 100% rename from database/migrations/2024_09_25_152710_create_categories_table.php rename to database/migrations/2020_09_25_152710_create_categories_table.php diff --git a/database/migrations/2021_03_17_105310_create_questions_table.php b/database/migrations/2021_03_17_105310_create_questions_table.php index 076a3f27..7009155c 100644 --- a/database/migrations/2021_03_17_105310_create_questions_table.php +++ b/database/migrations/2021_03_17_105310_create_questions_table.php @@ -13,7 +13,7 @@ class CreateQuestionsTable extends Migration */ public function up() { - Schema::create('question', function (Blueprint $table) { + Schema::create('questions', function (Blueprint $table) { $table->id(); $table->foreignId('category_id')->constrained()->onDelete('cascade'); $table->text('text'); diff --git a/database/migrations/2021_03_17_112752_create_answers_table.php b/database/migrations/2021_03_17_112752_create_answers_table.php index a0b0f8a9..aff244e3 100644 --- a/database/migrations/2021_03_17_112752_create_answers_table.php +++ b/database/migrations/2021_03_17_112752_create_answers_table.php @@ -13,7 +13,7 @@ class CreateAnswersTable extends Migration */ public function up() { - Schema::create('answer', function (Blueprint $table) { + Schema::create('answers', function (Blueprint $table) { $table->id(); $table->foreignId('question_id')->constrained()->onDelete('cascade'); $table->text('answer_text'); diff --git a/node_modules/public-encrypt/test/test_rsa_privkey.pem b/node_modules/public-encrypt/test/test_rsa_privkey.pem new file mode 100644 index 00000000..425518a0 --- /dev/null +++ b/node_modules/public-encrypt/test/test_rsa_privkey.pem @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF +NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F +UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB +AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ+UuvMNUCdN2QPc4sBiA +QWvLw1cSKt5DsKZ8UETpYPy8pPYnnDEz2dDYiaew9+xEpubyeW2oH4Zx71wqBtOK +kqwrXa/pzdpiucRRjk6vE6YY7EBBs/g7uanVpGibOVAEsqH1AkEA7DkjVH28WDUg +f1nqvfn2Kj6CT7nIcE3jGJsZZ7zlZmBmHFDONMLUrXR/Zm3pR5m0tCmBqa5RK95u +412jt1dPIwJBANJT3v8pnkth48bQo/fKel6uEYyboRtA5/uHuHkZ6FQF7OUkGogc +mSJluOdc5t6hI1VsLn0QZEjQZMEOWr+wKSMCQQCC4kXJEsHAve77oP6HtG/IiEn7 +kpyUXRNvFsDE0czpJJBvL/aRFUJxuRK91jhjC68sA7NsKMGg5OXb5I5Jj36xAkEA +gIT7aFOYBFwGgQAQkWNKLvySgKbAZRTeLBacpHMuQdl1DfdntvAyqpAZ0lY0RKmW +G6aFKaqQfOXKCyWoUiVknQJAXrlgySFci/2ueKlIE1QqIiLSZ8V8OlpFLRnb1pzI +7U1yQXnTAEFYM560yJlzUpOb1V4cScGd365tiSMvxLOvTA== +-----END RSA PRIVATE KEY----- diff --git a/resources/css/app.css b/resources/css/app.css index bd6213e1..b5c61c95 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -1,3 +1,3 @@ @tailwind base; @tailwind components; -@tailwind utilities; \ No newline at end of file +@tailwind utilities; diff --git a/resources/views/app.blade.php b/resources/views/app.blade.php index ededf453..32fe6445 100644 --- a/resources/views/app.blade.php +++ b/resources/views/app.blade.php @@ -1,16 +1,46 @@ + -