Skip to content

Commit

Permalink
Attempt to replace default guest middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
edgrosvenor committed Feb 14, 2022
1 parent 04f6211 commit 328e0ee
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
26 changes: 26 additions & 0 deletions src/HandleAuthenticatedUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Grosv\LaravelPasswordlessLogin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HandleAuthenticatedUsers
{
public function handle(Request $request, \Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
$home = class_exists(\App\Providers\RouteServiceProvider::class)
? \App\Providers\RouteServiceProvider::HOME
: config('laravel-passwordless-login.redirect_on_success', '/');

return redirect($request->get('redirect_to', $home));
}
}

return $next($request);
}
}
3 changes: 2 additions & 1 deletion src/routes.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

use Grosv\LaravelPasswordlessLogin\LaravelPasswordlessLoginController;
use Grosv\LaravelPasswordlessLogin\HandleAuthenticatedUsers;
use Illuminate\Support\Facades\Route;

Route::get(
config('laravel-passwordless-login.login_route').'/{uid}',
[LaravelPasswordlessLoginController::class, 'login']
)->middleware(['web', 'guest'])->name(config('laravel-passwordless-login.login_route_name'));
)->middleware(['web', HandleAuthenticatedUsers::class])->name(config('laravel-passwordless-login.login_route_name'));

Route::get('/laravel_passwordless_login_redirect_test_route', [LaravelPasswordlessLoginController::class, 'redirectTestRoute'])->middleware('auth');
Route::get('/laravel_passwordless_login_redirect_overridden_route', [LaravelPasswordlessLoginController::class, 'redirectTestRoute'])->middleware('auth');
36 changes: 26 additions & 10 deletions tests/SignedUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ public function setUp(): void

$faker = Faker::create();
$this->user = User::create([
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
]);

$this->model_user = ModelUser::create([
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
]);

Carbon::setTestNow();
Expand Down Expand Up @@ -89,14 +89,14 @@ public function an_invalid_signature_request_will_not_log_user_in()
{
// Check 401 is returned
$this->assertGuest();
$response = $this->get($this->url.'tampered');
$response = $this->get($this->url . 'tampered');
$response->assertStatus(401);
$this->assertGuest();

// Check correct exception is thrown
$this->withoutExceptionHandling();
$this->expectException(InvalidSignatureException::class);
$this->get($this->url.'tampered');
$this->get($this->url . 'tampered');
}

/** @test */
Expand Down Expand Up @@ -139,4 +139,20 @@ public function an_expired_request_will_not_log_user_in()
$this->get($this->url);
$this->assertGuest();
}

/** @test */
public function an_authenticated_user_is_redirected_correctly()
{
$this->actingAs($this->user);
$response = $this->get($this->url);
$response->assertRedirect(config('laravel-passwordless-login.redirect_on_success'));
}

/** @test */
public function an_authenticated_user_with_redirect_on_url_is_redirected_correctly()
{
$this->actingAs($this->user);
$response = $this->get($this->url . '&redirect_to=/happy_path');
$response->assertRedirect('/happy_path');
}
}

0 comments on commit 328e0ee

Please sign in to comment.