feature - 20 - demo auto login
This commit is contained in:
parent
4b31f3d315
commit
c80891b25e
9 changed files with 101 additions and 14 deletions
|
|
@ -6,6 +6,7 @@ enum AppModeEnum: string
|
|||
{
|
||||
case APP = 'app';
|
||||
case SAAS = 'saas';
|
||||
case DEMO = 'demo';
|
||||
|
||||
public static function current(): self
|
||||
{
|
||||
|
|
@ -21,4 +22,19 @@ public function isSaas(): bool
|
|||
{
|
||||
return $this === self::SAAS;
|
||||
}
|
||||
|
||||
public function isDemo(): bool
|
||||
{
|
||||
return $this === self::DEMO;
|
||||
}
|
||||
|
||||
public function requiresSubscription(): bool
|
||||
{
|
||||
return $this === self::SAAS;
|
||||
}
|
||||
|
||||
public function allowsLogout(): bool
|
||||
{
|
||||
return $this !== self::DEMO;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,6 +34,10 @@ public function login(Request $request)
|
|||
|
||||
public function logout(Request $request)
|
||||
{
|
||||
if (is_mode_demo()) {
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
|
||||
Auth::logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
|
|
|
|||
35
app/Http/Middleware/DemoMiddleware.php
Normal file
35
app/Http/Middleware/DemoMiddleware.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Planner;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class DemoMiddleware
|
||||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (! is_mode_demo()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if (Auth::check()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$planner = Planner::create([
|
||||
'name' => 'Demo User',
|
||||
'email' => 'demo-' . Str::uuid() . '@demo.local',
|
||||
'password' => Hash::make(Str::random(32)),
|
||||
]);
|
||||
|
||||
Auth::login($planner);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Enums\AppModeEnum;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
|
@ -10,7 +11,7 @@ class RequireSubscription
|
|||
{
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (is_mode_app()) {
|
||||
if (! AppModeEnum::current()->requiresSubscription()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* @property int $id
|
||||
* @property static PlannerFactory factory($count = null, $state = [])
|
||||
* @method static first()
|
||||
* @method static create(array $array)
|
||||
*/
|
||||
class Planner extends Authenticatable
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,4 +14,18 @@ function is_mode_saas(): bool
|
|||
{
|
||||
return AppModeEnum::current()->isSaas();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('is_mode_demo')) {
|
||||
function is_mode_demo(): bool
|
||||
{
|
||||
return AppModeEnum::current()->isDemo();
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('allows_logout')) {
|
||||
function allows_logout(): bool
|
||||
{
|
||||
return AppModeEnum::current()->allowsLogout();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Http\Middleware\DemoMiddleware;
|
||||
use App\Http\Middleware\ForceJsonResponse;
|
||||
use App\Http\Middleware\RequireSaasMode;
|
||||
use App\Http\Middleware\RequireSubscription;
|
||||
|
|
@ -27,7 +28,7 @@
|
|||
->withMiddleware(function (Middleware $middleware) {
|
||||
// Apply ForceJsonResponse only to API routes
|
||||
$middleware->api(ForceJsonResponse::class);
|
||||
|
||||
$middleware->web(DemoMiddleware::class);
|
||||
$middleware->alias([
|
||||
'subscription' => RequireSubscription::class,
|
||||
'saas' => RequireSaasMode::class,
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@
|
|||
*/
|
||||
|
||||
'mode' => env('APP_MODE', 'app'),
|
||||
'demo_subscribe_url' => env('APP_DEMO_SUBSCRIBE_URL', 'https://dishplanner.app'),
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -13,6 +13,16 @@
|
|||
</head>
|
||||
<body class="font-sans antialiased bg-gray-600 text-gray-100" x-data="{ mobileMenuOpen: false }">
|
||||
<div class="min-h-screen">
|
||||
@if(is_mode_demo())
|
||||
<!-- Demo Banner -->
|
||||
<div class="bg-primary text-white text-center py-2 px-4 text-sm sticky top-0 z-[60]">
|
||||
DEMO MODE.
|
||||
<a href="{{ config('app.demo_subscribe_url') }}" target="_blank" rel="noopener noreferrer" class="underline font-semibold hover:text-gray-200 ml-1">
|
||||
Subscribe for only €1 / month →
|
||||
</a>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<!-- Navigation -->
|
||||
<nav class="border-b-2 border-secondary shadow-sm z-50 mb-8 bg-gray-700">
|
||||
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||
|
|
@ -70,12 +80,14 @@ class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg bg-gray-
|
|||
Billing
|
||||
</a>
|
||||
@endif
|
||||
<form method="POST" action="{{ route('logout') }}">
|
||||
@csrf
|
||||
<button type="submit" class="block w-full text-left px-4 py-2 text-sm text-gray-100 hover:bg-gray-600 hover:text-accent-blue">
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
@if(allows_logout())
|
||||
<form method="POST" action="{{ route('logout') }}">
|
||||
@csrf
|
||||
<button type="submit" class="block w-full text-left px-4 py-2 text-sm text-gray-100 hover:bg-gray-600 hover:text-accent-blue">
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -150,12 +162,14 @@ class="block text-2xl font-medium {{ request()->routeIs('schedule.*') ? 'text-ac
|
|||
Billing
|
||||
</a>
|
||||
@endif
|
||||
<form method="POST" action="{{ route('logout') }}">
|
||||
@csrf
|
||||
<button type="submit" class="text-xl text-danger">
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
@if(allows_logout())
|
||||
<form method="POST" action="{{ route('logout') }}">
|
||||
@csrf
|
||||
<button type="submit" class="text-xl text-danger">
|
||||
Logout
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
</div>
|
||||
@else
|
||||
<div class="space-y-6 text-center">
|
||||
|
|
|
|||
Loading…
Reference in a new issue