Skip to content

Commit

Permalink
fix login logout flow
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKisb committed Sep 6, 2024
1 parent f596722 commit 575a6c9
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 19 deletions.
46 changes: 46 additions & 0 deletions app/Http/Responses/LoginResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginResponse implements LoginResponseContract
{
protected $roleRedirects = [
'admin' => '/admin',
'free' => '/app',
];

protected function shouldRedirect(Request $request, $redirect)
{
// Check if the current request path matches the redirect path
return !$request->is($redirect) && !$request->is($redirect . '/*');
}

public function toResponse($request)
{
setPermissionsTeamId(Auth::user()->current_team_id);
$user = Auth::user();

foreach ($this->roleRedirects as $role => $redirect) {
if ($user->hasRole($role)) {
return $request->wantsJson()
? new JsonResponse(['two_factor' => false], 200)
: ($this->shouldRedirect($request, $redirect)
? redirect()->to($redirect)
: redirect()->intended($redirect));
}
}

// Default redirection
$redirect = '/app';
return $request->wantsJson()
? new JsonResponse(['two_factor' => false], 200)
: ($this->shouldRedirect($request, $redirect)
? redirect()->to($redirect)
: redirect()->intended($redirect));
}
}
14 changes: 14 additions & 0 deletions app/Http/Responses/LogoutResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Responses;

use Filament\Http\Responses\Auth\Contracts\LogoutResponse as Responsable;
use Illuminate\Http\RedirectResponse;

class LogoutResponse implements Responsable
{
public function toResponse($request): RedirectResponse
{
return redirect('/login');
}
}
51 changes: 51 additions & 0 deletions app/Http/Responses/RegisterResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Responses;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
use Illuminate\Support\Facades\Auth;

class RegisterResponse implements RegisterResponseContract
{
protected $roleRedirects = [
'admin' => '/admin',
'free' => '/app',
];

protected function shouldRedirect(Request $request, $redirect)
{
// Check if the current request path matches the redirect path
return !$request->is($redirect) && !$request->is($redirect . '/*');
}

/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
public function toResponse($request)
{
setPermissionsTeamId(Auth::user()->current_team_id);
$user = Auth::user();

// Check if the user has a role and redirect accordingly
foreach ($this->roleRedirects as $role => $redirect) {
if ($user->hasRole($role)) {
return $request->wantsJson()
? new JsonResponse(['two_factor' => false], 200)
: ($this->shouldRedirect($request, $redirect)
? redirect()->to($redirect)
: redirect()->intended($redirect));
}
}

// Default redirection
$redirect = '/app';
return $request->wantsJson()
? new JsonResponse(['two_factor' => false], 200)
: ($this->shouldRedirect($request, $redirect)
? redirect()->to($redirect)
: redirect()->intended($redirect));
}
}
8 changes: 5 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ public function profilePhotoUrl(): Attribute
*/
public function getTenants(Panel $panel): array|Collection
{
return $this->ownedTeams;
return $this->teams;
}

public function canAccessTenant(Model $tenant): bool
{
return true; //$this->ownedTeams->contains($tenant);
return $this->teams->contains($tenant);
}

public function canAccessPanel(Panel $panel): bool
{
// return $this->hasVerifiedEmail();
if ($panel->getId() === "admin") {
return $this->hasRole('admin');
}
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions app/Providers/Filament/AdminPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function panel(Panel $panel): Panel
->default()
->id('admin')
->path('admin')
->login([AuthenticatedSessionController::class, 'create'])
->passwordReset()
->emailVerification()
// ->login([AuthenticatedSessionController::class, 'create'])
// ->passwordReset()
// ->emailVerification()
->viteTheme('resources/css/filament/admin/theme.css')
->colors([
'primary' => Color::Gray,
Expand Down
8 changes: 4 additions & 4 deletions app/Providers/Filament/AppPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function panel(Panel $panel): Panel
->default()
->id('app')
->path('app')
->login([AuthenticatedSessionController::class, 'create'])
->registration()
->passwordReset()
->emailVerification()
// ->login([AuthenticatedSessionController::class, 'create'])
// ->registration()
// ->passwordReset()
// ->emailVerification()
->viteTheme('resources/css/filament/admin/theme.css')
->colors([
'primary' => Color::Gray,
Expand Down
8 changes: 8 additions & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Http\Responses\LoginResponse;
use App\Http\Responses\LogoutResponse;
use App\Http\Responses\RegisterResponse;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Fortify\Fortify;
use Filament\Http\Responses\Auth\Contracts\LogoutResponse as LogoutResponseContract;

class FortifyServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -42,5 +46,9 @@ public function boot(): void
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});

$this->app->bind(LogoutResponseContract::class, LogoutResponse::class);
$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponse::class, LoginResponse::class);
$this->app->singleton(\Laravel\Fortify\Contracts\RegisterResponse::class, RegisterResponse::class);
}
}
10 changes: 2 additions & 8 deletions resources/views/components/home-navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@if (auth()->check())
@php
$user = auth()->user();
$role = $user->getRoleNames()->first() ?? 'user';
$dashboardUrl = $role === 'admin' ? '/admin' : '/' . $role;
$role = $user->hasRole('admin') ? 'admin' : 'user';
$dashboardUrl = $role === 'admin' ? '/admin' : '/app';
@endphp
@endif
<nav class="bg-white border-gray-200 dark:bg-gray-900">
Expand All @@ -28,12 +28,6 @@ class="flex flex-col font-medium p-4 md:p-0 mt-4 border border-gray-100 rounded-

<div class="flex items-center space-x-3 rtl:space-x-reverse">
@if (auth()->check())
@php
$user = auth()->user();
$role = $user->getRoleNames()->first() ?? 'user';
$dashboardUrl = $role === 'admin' ? '/admin' : '/' . $role;
@endphp

<a href="{{ $dashboardUrl }}"
class="hover:text-blue-700 px-3 py-2 rounded-md text-sm font-medium lg:hidden">
{{ ucfirst($role) }} Dashboard
Expand Down
2 changes: 1 addition & 1 deletion resources/views/welcome.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@if (Route::has('login'))
<div class="sm:fixed sm:top-0 sm:right-0 p-6 text-right z-10">
@auth
<a href="{{ url('/dashboard') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Dashboard</a>
<a href="{{ url('/app') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Dashboard</a>
@else
<a href="{{ route('login') }}" class="font-semibold text-gray-600 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white focus:outline focus:outline-2 focus:rounded-sm focus:outline-red-500">Log in</a>

Expand Down

0 comments on commit 575a6c9

Please sign in to comment.