diff --git a/app/Http/Controllers/Collaborators/Auth/LogoutController.php b/app/Http/Controllers/Collaborators/Auth/LogoutController.php index 98c8fd2..911b5df 100644 --- a/app/Http/Controllers/Collaborators/Auth/LogoutController.php +++ b/app/Http/Controllers/Collaborators/Auth/LogoutController.php @@ -11,8 +11,8 @@ class LogoutController extends Controller { public function __invoke(): RedirectResponse { - Auth::logout(); + Auth::guard('collaborators')->logout(); - return redirect(route('home')); + return redirect(route('collaborators.home')); } } diff --git a/app/Http/Livewire/Collaborators/Auth/Login.php b/app/Http/Livewire/Collaborators/Auth/Login.php index f2f18c5..88fcf86 100644 --- a/app/Http/Livewire/Collaborators/Auth/Login.php +++ b/app/Http/Livewire/Collaborators/Auth/Login.php @@ -24,13 +24,13 @@ class Login extends Component 'password' => ['required'], ]); - if (!Auth::attempt($credentials, $this->remember)) { + if (!Auth::guard('collaborators')->attempt($credentials, $this->remember)) { $this->addError('email', trans('auth.failed')); return; } - redirect(route('home')); + redirect(route('collaborators.home')); } public function render() diff --git a/app/Http/Livewire/Collaborators/Auth/Passwords/Email.php b/app/Http/Livewire/Collaborators/Auth/Passwords/Email.php index 519a6e3..ae36442 100644 --- a/app/Http/Livewire/Collaborators/Auth/Passwords/Email.php +++ b/app/Http/Livewire/Collaborators/Auth/Passwords/Email.php @@ -37,7 +37,7 @@ class Email extends Component */ public function broker() { - return Password::broker(); + return Password::broker('collaborators'); } public function render() diff --git a/app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php b/app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php index 5a3e632..17d44ca 100644 --- a/app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php +++ b/app/Http/Livewire/Collaborators/Auth/Passwords/Reset.php @@ -59,7 +59,7 @@ class Reset extends Component if ($response == Password::PASSWORD_RESET) { session()->flash(trans($response)); - return redirect(route('home')); + return redirect(route('collaborators.home')); } $this->addError('email', trans($response)); @@ -72,7 +72,7 @@ class Reset extends Component */ public function broker() { - return Password::broker(); + return Password::broker('collaborators'); } /** @@ -82,7 +82,7 @@ class Reset extends Component */ protected function guard() { - return Auth::guard(); + return Auth::guard('collaborators'); } public function render() diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 704089a..46fcfee 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -15,7 +15,11 @@ class Authenticate extends Middleware protected function redirectTo($request) { if (! $request->expectsJson()) { - return route('login'); + if ($request->is('admin/*')) { + return route('collaborators.login'); + } else { + return route('login'); + } } } } diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index df4abc5..638f3ae 100644 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -19,7 +19,11 @@ class RedirectIfAuthenticated public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { - return redirect(route('home')); + if ($guard === 'collaborators') { + return redirect(route('collaborators.home')); + } else { + return redirect(route('home')); + } } return $next($request); diff --git a/resources/views/livewire/collaborators/auth/login.blade.php b/resources/views/livewire/collaborators/auth/login.blade.php index f6e91f4..d0abb33 100644 --- a/resources/views/livewire/collaborators/auth/login.blade.php +++ b/resources/views/livewire/collaborators/auth/login.blade.php @@ -7,12 +7,6 @@

{{ __('auth.login') }}

-

- Ou - - {{ __('auth.register') }} - -

diff --git a/routes/web.php b/routes/web.php index 9512364..5010608 100644 --- a/routes/web.php +++ b/routes/web.php @@ -29,7 +29,7 @@ Route::middleware('auth')->group(function () { }); Route::prefix('admin')->name('collaborators.')->group(function () { - Route::middleware('guest')->group(function () { + Route::middleware('guest:collaborators')->group(function () { Route::view('login', 'collaborators.auth.login')->name('login'); }); diff --git a/tests/Feature/Collaborators/Auth/LoginTest.php b/tests/Feature/Collaborators/Auth/LoginTest.php new file mode 100644 index 0000000..d708e05 --- /dev/null +++ b/tests/Feature/Collaborators/Auth/LoginTest.php @@ -0,0 +1,107 @@ +get(route('collaborators.login')) + ->assertSuccessful() + ->assertSeeLivewire('collaborators.auth.login'); + } + + /** @test */ + public function is_redirected_if_already_logged_in() + { + $collaborator = factory(Collaborator::class)->create(); + + $this->be($collaborator, 'collaborators'); + + $this->get(route('collaborators.login')) + ->assertRedirect(route('collaborators.home')); + } + + /** @test */ + public function a_collaborator_can_login() + { + $collaborator = $this->factoryWithoutObservers(Collaborator::class)->create(['password' => Hash::make('password')]); + + Livewire::test('collaborators.auth.login') + ->set('email', $collaborator->email) + ->set('password', 'password') + ->call('authenticate'); + + $this->assertAuthenticatedAs($collaborator, 'collaborators'); + } + + /** @test */ + public function is_redirected_to_the_home_page_after_login() + { + $collaborator = $this->factoryWithoutObservers(Collaborator::class)->create(['password' => Hash::make('password')]); + + Livewire::test('collaborators.auth.login') + ->set('email', $collaborator->email) + ->set('password', 'password') + ->call('authenticate') + ->assertRedirect(route('collaborators.home')); + } + + /** @test */ + public function email_is_required() + { + $collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]); + + Livewire::test('collaborators.auth.login') + ->set('password', 'password') + ->call('authenticate') + ->assertHasErrors(['email' => 'required']); + } + + /** @test */ + public function email_must_be_valid_email() + { + $collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]); + + Livewire::test('collaborators.auth.login') + ->set('email', 'invalid-email') + ->set('password', 'password') + ->call('authenticate') + ->assertHasErrors(['email' => 'email']); + } + + /** @test */ + public function password_is_required() + { + $collaborator = factory(Collaborator::class)->create(['password' => Hash::make('password')]); + + Livewire::test('collaborators.auth.login') + ->set('email', $collaborator->email) + ->call('authenticate') + ->assertHasErrors(['password' => 'required']); + } + + /** @test */ + public function bad_login_attempt_shows_message() + { + $collaborator = factory(Collaborator::class)->create(); + + Livewire::test('collaborators.auth.login') + ->set('email', $collaborator->email) + ->set('password', 'bad-password') + ->call('authenticate') + ->assertHasErrors('email'); + + $this->assertFalse(Auth::check()); + } +} diff --git a/tests/Feature/Collaborators/Auth/LogoutTest.php b/tests/Feature/Collaborators/Auth/LogoutTest.php new file mode 100644 index 0000000..271cdcc --- /dev/null +++ b/tests/Feature/Collaborators/Auth/LogoutTest.php @@ -0,0 +1,34 @@ +create(); + $this->be($collaborator, 'collaborators'); + + $this->post(route('collaborators.logout')) + ->assertRedirect(route('collaborators.home')); + + $this->assertFalse(Auth::check('collaborators')); + } + + /** @test */ + public function an_unauthenticated_collaborator_can_not_log_out() + { + $this->post(route('collaborators.logout')) + ->assertRedirect(route('collaborators.login')); + + $this->assertFalse(Auth::check('collaborators')); + } +} diff --git a/tests/Feature/Collaborators/Auth/Passwords/EmailTest.php b/tests/Feature/Collaborators/Auth/Passwords/EmailTest.php new file mode 100644 index 0000000..029fd24 --- /dev/null +++ b/tests/Feature/Collaborators/Auth/Passwords/EmailTest.php @@ -0,0 +1,53 @@ +get(route('collaborators.password.request')) + ->assertSuccessful() + ->assertSeeLivewire('collaborators.auth.passwords.email'); + } + + /** @test */ + public function a_collaborator_must_enter_an_email_address() + { + Livewire::test('collaborators.auth.passwords.email') + ->call('sendResetPasswordLink') + ->assertHasErrors(['email' => 'required']); + } + + /** @test */ + public function a_collaborator_must_enter_a_valid_email_address() + { + Livewire::test('collaborators.auth.passwords.email') + ->set('email', 'email') + ->call('sendResetPasswordLink') + ->assertHasErrors(['email' => 'email']); + } + + /** @test */ + public function a_collaborator_who_enters_a_valid_email_address_will_get_sent_an_email() + { + $collaborator = factory(Collaborator::class)->create(); + + Livewire::test('collaborators.auth.passwords.email') + ->set('email', $collaborator->email) + ->call('sendResetPasswordLink') + ->assertNotSet('emailSentMessage', false); + + $this->assertDatabaseHas('collaborator_password_resets', [ + 'email' => $collaborator->email, + ]); + } +} diff --git a/tests/Feature/Collaborators/Auth/Passwords/ResetTest.php b/tests/Feature/Collaborators/Auth/Passwords/ResetTest.php new file mode 100644 index 0000000..fefc2b4 --- /dev/null +++ b/tests/Feature/Collaborators/Auth/Passwords/ResetTest.php @@ -0,0 +1,132 @@ +create(); + + $token = Str::random(16); + + DB::table('collaborator_password_resets')->insert([ + 'email' => $collaborator->email, + 'token' => Hash::make($token), + 'created_at' => Carbon::now(), + ]); + + $this->get(route('collaborators.password.reset', [ + 'email' => $collaborator->email, + 'token' => $token, + ])) + ->assertSuccessful() + ->assertSeeLivewire('collaborators.auth.passwords.reset'); + } + + /** @test */ + public function can_reset_password() + { + $collaborator = factory(Collaborator::class)->create(); + + $token = Str::random(16); + + DB::table('collaborator_password_resets')->insert([ + 'email' => $collaborator->email, + 'token' => Hash::make($token), + 'created_at' => Carbon::now(), + ]); + + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => $token, + ]) + ->set('email', $collaborator->email) + ->set('password', 'new-password') + ->set('passwordConfirmation', 'new-password') + ->call('resetPassword'); + + $this->assertTrue(Auth::guard('collaborators')->attempt([ + 'email' => $collaborator->email, + 'password' => 'new-password', + ])); + } + + /** @test */ + public function token_is_required() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => null, + ]) + ->call('resetPassword') + ->assertHasErrors(['token' => 'required']); + } + + /** @test */ + public function email_is_required() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => Str::random(16), + ]) + ->set('email', null) + ->call('resetPassword') + ->assertHasErrors(['email' => 'required']); + } + + /** @test */ + public function email_is_valid_email() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => Str::random(16), + ]) + ->set('email', 'email') + ->call('resetPassword') + ->assertHasErrors(['email' => 'email']); + } + + /** @test */ + function password_is_required() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => Str::random(16), + ]) + ->set('password', '') + ->call('resetPassword') + ->assertHasErrors(['password' => 'required']); + } + + /** @test */ + function password_is_minimum_of_eight_characters() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => Str::random(16), + ]) + ->set('password', 'secret') + ->call('resetPassword') + ->assertHasErrors(['password' => 'min']); + } + + /** @test */ + function password_matches_password_confirmation() + { + Livewire::test('collaborators.auth.passwords.reset', [ + 'token' => Str::random(16), + ]) + ->set('password', 'new-password') + ->set('passwordConfirmation', 'not-new-password') + ->call('resetPassword') + ->assertHasErrors(['password' => 'same']); + } +}