Skip to content

Commit

Permalink
Stop discord users from logging in using a password
Browse files Browse the repository at this point in the history
  • Loading branch information
LouiseMcMahon committed Feb 4, 2025
1 parent d5b8598 commit d7e74cc
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 162 deletions.
41 changes: 0 additions & 41 deletions app/Http/Controllers/Auth/ConfirmablePasswordController.php

This file was deleted.

11 changes: 10 additions & 1 deletion app/Http/Controllers/Auth/PasswordResetLinkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
Expand Down Expand Up @@ -33,11 +34,19 @@ public function store(Request $request): RedirectResponse
'email' => 'required|email',
]);

$user = User::where('email', $request->get('email'))->first();

if ($user->discordUser !== null) {
throw ValidationException::withMessages([
'email' => [trans(Password::INVALID_USER)],
]);
}

// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$status = Password::sendResetLink(
$request->only('email')
$request->only('email'),
);

if ($status == Password::RESET_LINK_SENT) {
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Requests/Auth/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests\Auth;

use App\Models\User;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -41,7 +42,13 @@ public function authenticate(): void
{
$this->ensureIsNotRateLimited();

if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
if (! Auth::attemptWhen(
$this->only('email', 'password'),
function (User $user) {
return $user->discordUser === null;
},
$this->boolean('remember')
)) {
RateLimiter::hit($this->throttleKey());

throw ValidationException::withMessages([
Expand Down
63 changes: 0 additions & 63 deletions resources/js/Pages/Auth/ConfirmPassword.vue

This file was deleted.

3 changes: 2 additions & 1 deletion resources/js/Pages/Auth/ForgotPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import InputError from '@/Components/InputError.vue';
import InputLabel from '@/Components/InputLabel.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import TextInput from '@/Components/TextInput.vue';
import { Head, useForm } from '@inertiajs/vue3';
import {Head, useForm} from '@inertiajs/vue3';
defineProps<{
status?: string;
Expand Down Expand Up @@ -63,6 +63,7 @@ const submit = () => {
<PrimaryButton
:class="{ 'opacity-25': form.processing }"
:disabled="form.processing"
type="submit"
>
Email Password Reset Link
</PrimaryButton>
Expand Down
1 change: 1 addition & 0 deletions resources/js/Pages/Auth/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const showEmailForm = ref(false);
class="ms-4 bg-brand"
:class="{ 'opacity-25': form.processing }"
:disabled="form.processing"
type="submit"
>
Log in
</PrimaryButton>
Expand Down
3 changes: 2 additions & 1 deletion resources/js/Pages/Auth/ResetPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import InputError from '@/Components/InputError.vue';
import InputLabel from '@/Components/InputLabel.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';
import TextInput from '@/Components/TextInput.vue';
import { Head, useForm } from '@inertiajs/vue3';
import {Head, useForm} from '@inertiajs/vue3';
const props = defineProps<{
Expand Down Expand Up @@ -101,6 +101,7 @@ const submit = () => {
<PrimaryButton
:class="{ 'opacity-25': form.processing }"
:disabled="form.processing"
type="submit"
>
Reset Password
</PrimaryButton>
Expand Down
6 changes: 0 additions & 6 deletions routes/auth.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\ConfirmablePasswordController;
use App\Http\Controllers\Auth\DiscordController;
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
Expand Down Expand Up @@ -49,11 +48,6 @@
->middleware('throttle:6,1')
->name('verification.send');

Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
->name('password.confirm');

Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);

Route::put('password', [PasswordController::class, 'update'])->name('password.update');

Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Route::get('/', function () {
return redirect()->route('login');
});
})->name('home');

Route::get('/dashboard', function () {
return Inertia::render('Dashboard');
Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\Auth;

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Models\DiscordUser;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\CoversClass;
Expand Down Expand Up @@ -54,4 +55,22 @@ public function test_users_can_logout(): void
$this->assertGuest();
$response->assertRedirect('/');
}

public function test_user_with_discord_login_cannot_login_with_password(): void
{
$user = User::factory([
'password' => null,
'remember_token' => null,
])
->has(DiscordUser::factory())
->create();

$response = $this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);

$this->assertGuest();
$response->assertRedirect(route('home'));
}
}
47 changes: 0 additions & 47 deletions tests/Feature/Auth/PasswordConfirmationTest.php

This file was deleted.

17 changes: 17 additions & 0 deletions tests/Feature/Auth/PasswordResetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature\Auth;

use App\Http\Controllers\Auth\NewPasswordController;
use App\Models\DiscordUser;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
Expand Down Expand Up @@ -33,6 +34,22 @@ public function test_reset_password_link_can_be_requested(): void
Notification::assertSentTo($user, ResetPassword::class);
}

public function test_reset_password_link_cannot_be_requested_for_user_with_discord_login(): void
{
Notification::fake();

$user = User::factory([
'password' => null,
'remember_token' => null,
])
->has(DiscordUser::factory())
->create();

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertNotSentTo($user, ResetPassword::class);
}

public function test_reset_password_screen_can_be_rendered(): void
{
Notification::fake();
Expand Down

0 comments on commit d7e74cc

Please sign in to comment.