diff --git a/stubs/default/App/Http/Controllers/Auth/AuthenticatedSessionController.php b/stubs/default/App/Http/Controllers/Auth/AuthenticatedSessionController.php new file mode 100644 index 0000000..09abe87 --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/AuthenticatedSessionController.php @@ -0,0 +1,54 @@ +authenticate(); + + $request->session()->regenerate(); + + return redirect()->intended(RouteServiceProvider::HOME); + } + + /** + * Destroy an authenticated session. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\RedirectResponse + */ + public function destroy(Request $request) + { + Auth::guard('web')->logout(); + + $request->session()->invalidate(); + + $request->session()->regenerateToken(); + + return redirect('/'); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/ConfirmablePasswordController.php b/stubs/default/App/Http/Controllers/Auth/ConfirmablePasswordController.php new file mode 100644 index 0000000..1175010 --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/ConfirmablePasswordController.php @@ -0,0 +1,44 @@ +validate([ + 'email' => $request->user()->email, + 'password' => $request->password, + ])) { + throw ValidationException::withMessages([ + 'password' => __('auth.password'), + ]); + } + + $request->session()->put('auth.password_confirmed_at', time()); + + return redirect()->intended(RouteServiceProvider::HOME); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/EmailVerificationNotificationController.php b/stubs/default/App/Http/Controllers/Auth/EmailVerificationNotificationController.php new file mode 100644 index 0000000..3362dca --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/EmailVerificationNotificationController.php @@ -0,0 +1,27 @@ +user()->hasVerifiedEmail()) { + return redirect()->intended(RouteServiceProvider::HOME); + } + + $request->user()->sendEmailVerificationNotification(); + + return back()->with('status', 'verification-link-sent'); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/EmailVerificationPromptController.php b/stubs/default/App/Http/Controllers/Auth/EmailVerificationPromptController.php new file mode 100644 index 0000000..e247f95 --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/EmailVerificationPromptController.php @@ -0,0 +1,23 @@ +user()->hasVerifiedEmail() + ? redirect()->intended(RouteServiceProvider::HOME) + : view('auth.verify-email'); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/NewPasswordController.php b/stubs/default/App/Http/Controllers/Auth/NewPasswordController.php new file mode 100644 index 0000000..e1c121f --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/NewPasswordController.php @@ -0,0 +1,65 @@ + $request]); + } + + /** + * Handle an incoming new password request. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\RedirectResponse + * + * @throws \Illuminate\Validation\ValidationException + */ + public function store(Request $request) + { + $request->validate([ + 'token' => 'required', + 'email' => 'required|email', + 'password' => ['required', 'confirmed', Rules\Password::min(8)], + ]); + + // Here we will attempt to reset the user's password. If it is successful we + // will update the password on an actual user model and persist it to the + // database. Otherwise we will parse the error and return the response. + $status = Password::reset( + $request->only('email', 'password', 'password_confirmation', 'token'), + function ($user) use ($request) { + $user->forceFill([ + 'password' => Hash::make($request->password), + 'remember_token' => Str::random(60), + ])->save(); + + event(new PasswordReset($user)); + } + ); + + // If the password was successfully reset, we will redirect the user back to + // the application's home authenticated view. If there is an error we can + // redirect them back to where they came from with their error message. + return $status == Password::PASSWORD_RESET + ? redirect()->route('login')->with('status', __($status)) + : back()->withInput($request->only('email')) + ->withErrors(['email' => __($status)]); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/PasswordResetLinkController.php b/stubs/default/App/Http/Controllers/Auth/PasswordResetLinkController.php new file mode 100644 index 0000000..5181588 --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/PasswordResetLinkController.php @@ -0,0 +1,47 @@ +validate([ + 'email' => 'required|email', + ]); + + // 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') + ); + + return $status == Password::RESET_LINK_SENT + ? back()->with('status', __($status)) + : back()->withInput($request->only('email')) + ->withErrors(['email' => __($status)]); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/RegisteredUserController.php b/stubs/default/App/Http/Controllers/Auth/RegisteredUserController.php new file mode 100644 index 0000000..0736984 --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/RegisteredUserController.php @@ -0,0 +1,54 @@ +validate([ + 'name' => 'required|string|max:255', + 'email' => 'required|string|email|max:255|unique:users', + 'password' => ['required', 'confirmed', Rules\Password::min(8)], + ]); + + $user = User::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => Hash::make($request->password), + ]); + + event(new Registered($user)); + + Auth::login($user); + + return redirect(RouteServiceProvider::HOME); + } +} diff --git a/stubs/default/App/Http/Controllers/Auth/VerifyEmailController.php b/stubs/default/App/Http/Controllers/Auth/VerifyEmailController.php new file mode 100644 index 0000000..6baa9aa --- /dev/null +++ b/stubs/default/App/Http/Controllers/Auth/VerifyEmailController.php @@ -0,0 +1,30 @@ +user()->hasVerifiedEmail()) { + return redirect()->intended(RouteServiceProvider::HOME.'?verified=1'); + } + + if ($request->user()->markEmailAsVerified()) { + event(new Verified($request->user())); + } + + return redirect()->intended(RouteServiceProvider::HOME.'?verified=1'); + } +} diff --git a/stubs/default/App/Http/Requests/Auth/LoginRequest.php b/stubs/default/App/Http/Requests/Auth/LoginRequest.php new file mode 100644 index 0000000..2143861 --- /dev/null +++ b/stubs/default/App/Http/Requests/Auth/LoginRequest.php @@ -0,0 +1,93 @@ + 'required|string|email', + 'password' => 'required|string', + ]; + } + + /** + * Attempt to authenticate the request's credentials. + * + * @return void + * + * @throws \Illuminate\Validation\ValidationException + */ + public function authenticate() + { + $this->ensureIsNotRateLimited(); + + if (! Auth::attempt($this->only('email', 'password'), $this->filled('remember'))) { + RateLimiter::hit($this->throttleKey()); + + throw ValidationException::withMessages([ + 'email' => __('auth.failed'), + ]); + } + + RateLimiter::clear($this->throttleKey()); + } + + /** + * Ensure the login request is not rate limited. + * + * @return void + * + * @throws \Illuminate\Validation\ValidationException + */ + public function ensureIsNotRateLimited() + { + if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) { + return; + } + + event(new Lockout($this)); + + $seconds = RateLimiter::availableIn($this->throttleKey()); + + throw ValidationException::withMessages([ + 'email' => trans('auth.throttle', [ + 'seconds' => $seconds, + 'minutes' => ceil($seconds / 60), + ]), + ]); + } + + /** + * Get the rate limiting throttle key for the request. + * + * @return string + */ + public function throttleKey() + { + return Str::lower($this->input('email')).'|'.$this->ip(); + } +} diff --git a/stubs/default/App/View/Components/AppLayout.php b/stubs/default/App/View/Components/AppLayout.php new file mode 100644 index 0000000..b45d342 --- /dev/null +++ b/stubs/default/App/View/Components/AppLayout.php @@ -0,0 +1,18 @@ +middleware('guest') + ->name('register'); + +Route::post('/register', [RegisteredUserController::class, 'store']) + ->middleware('guest'); + +Route::get('/login', [AuthenticatedSessionController::class, 'create']) + ->middleware('guest') + ->name('login'); + +Route::post('/login', [AuthenticatedSessionController::class, 'store']) + ->middleware('guest'); + +Route::get('/forgot-password', [PasswordResetLinkController::class, 'create']) + ->middleware('guest') + ->name('password.request'); + +Route::post('/forgot-password', [PasswordResetLinkController::class, 'store']) + ->middleware('guest') + ->name('password.email'); + +Route::get('/reset-password/{token}', [NewPasswordController::class, 'create']) + ->middleware('guest') + ->name('password.reset'); + +Route::post('/reset-password', [NewPasswordController::class, 'store']) + ->middleware('guest') + ->name('password.update'); + +Route::get('/verify-email', [EmailVerificationPromptController::class, '__invoke']) + ->middleware('auth') + ->name('verification.notice'); + +Route::get('/verify-email/{id}/{hash}', [VerifyEmailController::class, '__invoke']) + ->middleware(['auth', 'signed', 'throttle:6,1']) + ->name('verification.verify'); + +Route::post('/email/verification-notification', [EmailVerificationNotificationController::class, 'store']) + ->middleware(['auth', 'throttle:6,1']) + ->name('verification.send'); + +Route::get('/confirm-password', [ConfirmablePasswordController::class, 'show']) + ->middleware('auth') + ->name('password.confirm'); + +Route::post('/confirm-password', [ConfirmablePasswordController::class, 'store']) + ->middleware('auth'); + +Route::post('/logout', [AuthenticatedSessionController::class, 'destroy']) + ->middleware('auth') + ->name('logout'); diff --git a/stubs/default/routes/web.php b/stubs/default/routes/web.php new file mode 100644 index 0000000..852b11f --- /dev/null +++ b/stubs/default/routes/web.php @@ -0,0 +1,24 @@ +middleware(['auth'])->name('dashboard'); + +require __DIR__.'/auth.php'; diff --git a/stubs/default/tests/Feature/AuthenticationTest.php b/stubs/default/tests/Feature/AuthenticationTest.php new file mode 100644 index 0000000..2dbceac --- /dev/null +++ b/stubs/default/tests/Feature/AuthenticationTest.php @@ -0,0 +1,45 @@ +get('/login'); + + $response->assertStatus(200); + } + + public function test_users_can_authenticate_using_the_login_screen() + { + $user = User::factory()->create(); + + $response = $this->post('/login', [ + 'email' => $user->email, + 'password' => 'password', + ]); + + $this->assertAuthenticated(); + $response->assertRedirect(RouteServiceProvider::HOME); + } + + public function test_users_can_not_authenticate_with_invalid_password() + { + $user = User::factory()->create(); + + $this->post('/login', [ + 'email' => $user->email, + 'password' => 'wrong-password', + ]); + + $this->assertGuest(); + } +} diff --git a/stubs/default/tests/Feature/EmailVerificationTest.php b/stubs/default/tests/Feature/EmailVerificationTest.php new file mode 100644 index 0000000..61b5775 --- /dev/null +++ b/stubs/default/tests/Feature/EmailVerificationTest.php @@ -0,0 +1,65 @@ +create([ + 'email_verified_at' => null, + ]); + + $response = $this->actingAs($user)->get('/verify-email'); + + $response->assertStatus(200); + } + + public function test_email_can_be_verified() + { + Event::fake(); + + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $verificationUrl = URL::temporarySignedRoute( + 'verification.verify', + now()->addMinutes(60), + ['id' => $user->id, 'hash' => sha1($user->email)] + ); + + $response = $this->actingAs($user)->get($verificationUrl); + + Event::assertDispatched(Verified::class); + $this->assertTrue($user->fresh()->hasVerifiedEmail()); + $response->assertRedirect(RouteServiceProvider::HOME.'?verified=1'); + } + + public function test_email_is_not_verified_with_invalid_hash() + { + $user = User::factory()->create([ + 'email_verified_at' => null, + ]); + + $verificationUrl = URL::temporarySignedRoute( + 'verification.verify', + now()->addMinutes(60), + ['id' => $user->id, 'hash' => sha1('wrong-email')] + ); + + $this->actingAs($user)->get($verificationUrl); + + $this->assertFalse($user->fresh()->hasVerifiedEmail()); + } +} diff --git a/stubs/default/tests/Feature/PasswordConfirmationTest.php b/stubs/default/tests/Feature/PasswordConfirmationTest.php new file mode 100644 index 0000000..fdfe630 --- /dev/null +++ b/stubs/default/tests/Feature/PasswordConfirmationTest.php @@ -0,0 +1,44 @@ +create(); + + $response = $this->actingAs($user)->get('/confirm-password'); + + $response->assertStatus(200); + } + + public function test_password_can_be_confirmed() + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->post('/confirm-password', [ + 'password' => 'password', + ]); + + $response->assertRedirect(); + $response->assertSessionHasNoErrors(); + } + + public function test_password_is_not_confirmed_with_invalid_password() + { + $user = User::factory()->create(); + + $response = $this->actingAs($user)->post('/confirm-password', [ + 'password' => 'wrong-password', + ]); + + $response->assertSessionHasErrors(); + } +} diff --git a/stubs/default/tests/Feature/PasswordResetTest.php b/stubs/default/tests/Feature/PasswordResetTest.php new file mode 100644 index 0000000..8d98149 --- /dev/null +++ b/stubs/default/tests/Feature/PasswordResetTest.php @@ -0,0 +1,71 @@ +get('/forgot-password'); + + $response->assertStatus(200); + } + + public function test_reset_password_link_can_be_requested() + { + Notification::fake(); + + $user = User::factory()->create(); + + $this->post('/forgot-password', ['email' => $user->email]); + + Notification::assertSentTo($user, ResetPassword::class); + } + + public function test_reset_password_screen_can_be_rendered() + { + Notification::fake(); + + $user = User::factory()->create(); + + $this->post('/forgot-password', ['email' => $user->email]); + + Notification::assertSentTo($user, ResetPassword::class, function ($notification) { + $response = $this->get('/reset-password/'.$notification->token); + + $response->assertStatus(200); + + return true; + }); + } + + public function test_password_can_be_reset_with_valid_token() + { + Notification::fake(); + + $user = User::factory()->create(); + + $this->post('/forgot-password', ['email' => $user->email]); + + Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) { + $response = $this->post('/reset-password', [ + 'token' => $notification->token, + 'email' => $user->email, + 'password' => 'password', + 'password_confirmation' => 'password', + ]); + + $response->assertSessionHasNoErrors(); + + return true; + }); + } +} diff --git a/stubs/default/tests/Feature/RegistrationTest.php b/stubs/default/tests/Feature/RegistrationTest.php new file mode 100644 index 0000000..6dd5ff8 --- /dev/null +++ b/stubs/default/tests/Feature/RegistrationTest.php @@ -0,0 +1,32 @@ +get('/register'); + + $response->assertStatus(200); + } + + public function test_new_users_can_register() + { + $response = $this->post('/register', [ + 'name' => 'Test User', + 'email' => 'test@example.com', + 'password' => 'password', + 'password_confirmation' => 'password', + ]); + + $this->assertAuthenticated(); + $response->assertRedirect(RouteServiceProvider::HOME); + } +}