orchestral/testbench

Too Many Redirects on Serve with User Login

Closed this issue · 11 comments

  • Testbench Version: 8.11.1
  • Laravel Version: 10.24
  • PHP Version: 8.2
  • Database Driver & Version: SQLite (latest)

Description:

I run vendor/bin/testbench serve and get the browser error.

Too many redirects occurred trying to open 127.0.0.1:8001/_workbench

Steps To Reproduce:

testbench.yaml

providers:
  - Cjmellor\Rating\RatingServiceProvider
  - Livewire\LivewireServiceProvider

migrations: true
seeders: []

workbench:
  assets:
    - rating-config
  build:
    - asset-publish
    - create-sqlite-db
    - migrate:refresh
  guard: web
  install: true
  start: '/'
  sync: []
  user: 1
  welcome: true

Run:

composer clear

Run:

composer build

Create a User

# open tinker
vendor/bin/testbench tinker

# create user
Workbench\Database\Factories\UserFactory::new()->createOne();

Run:

vendor/bin/testbench server --port=8001`

Visit http://127.0.0.1:8001 and get redirect error

You should create a seeder:

vendor/bin/testbench make:seeder DatabaseSeeder

Add your seeder to testbench.yaml

seeder:
  - Workbench\Database\Seeders\DatabaseSeeder

Thanks for the pointer on how to use the Seeds properly, that works fine.

Unfortunately, the redirects are still occurring. Nothing looks out of the ordinary for me, which is why I'm struggling to debug it.

If I comment user guard and user it's fine, but I know defeats the point of needing to use it with a logged-in User.

Not sure if the value of APP_URL in workbench/.env matters? server goes to 127.0.0.1:8001 (:8000 already in use) and the value is http://127.0.0.1

Thanks

Do you need to set the start url to /? Do you have a authenticated route where the user can go to?

Yeah my testbench.yaml file is above. start is set to '/'

No, didn't set up any kind of auth route. Is it worth it? Would I just set up a routes/web.php file like in a regular app?

https://github.com/cjmellor/rating#livewire-component

serve would be useful to show a demo of how rating can be used in a Laravel app using workbench. Since the package doesn't have any route as a start page you might need to prepare an example page just using workbench as set that as the start page.

Yeah that's the plan 🙂

I added a routes/web.php file with a route to a /home path using auth middleware.

I ran serve and it redirects to /home now (updated the testbench.yaml) but it shows a 404.

I saw in the docs about a defineRoutes method that could be used but not sure if that's applicable here?

Is doing routes in workbench any different?

Thanks for helping me.

Just like any other Laravel application you need to register the routes/web.php using a Service Provider.

vendor/bin/testbench make:provider RouteServiceProvider

and register the route

Right, gotcha! I figured but wasn't sure if Workbench did that for you.

Thanks again

Hmm, so I tried this. I added a RouteServiceProvider class

<?php

namespace Workbench\App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        $this->routes(function () {
            Route::middleware('web')
                ->group(base_path('workbench/routes/web.php'));
        });
    }
}

The routes/web.php file is pretty simple:

<?php

use Illuminate\Support\Facades\Route;

Route::get(uri: '/home', action: fn (): string => 'Hello auth user!')
    ->middleware('auth');

Thought I might need to add an config/app.php file to register the Provider. I tried:

// workbench/config/app.php

'providers' => [
    \Workbench\App\Providers\RouteServiceProvider::class,
],

// as well as

'providers' => \Illuminate\Support\ServiceProvider::defaultProviders()->merge([
    \Workbench\App\Providers\RouteServiceProvider::class,
])->toArray(),

but to no avail.

Am I on the right track? Am I missing something?

Thanks

@crynobone Saw the 0.4.0 tag and what it accomplishes and everything is good now :)

I added

  discovers:
    web: true
    api: false
    commands: false

to testbench.yaml and changed the guard to auth as I need the user to be logged in... all seems good.

Thanks!

Yeah, still going through Testbench releases right now before I can update the docs.