Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Aimeos Authentication in existing app

Been working on making Aimeos work inside existing app but with Aimeos separate database. This seems to now cause issues with authentication When I check Laravel routes for store added with Aimeos package I see them:

art route:list --path=store

  POST      admin/users/{user}/restore ............................. admin.users.restore › Admin\UsersController@restore
  POST      smtv.test/dashboard/project-management/store ................... Dashboard\ProjectManagementController@store
  POST      smtv.test/dashboard/project-management/{project}/restore projects.restore › Dashboard\ProjectManagementCont…
  GET|HEAD  smtv.test/dashboard/store-admin .................................. Aimeos\Shop › AdminController@indexAction
  GET|HEAD  store-admin .................................. aimeos_shop_admin › Aimeos\Shop › AdminController@indexAction
  POST      store-admin/{site}/jqadm/batch/{resource} aimeos_shop_jqadm_batch › Aimeos\Shop › JqadmController@batchActi…
  GET|POST|HEAD store-admin/{site}/jqadm/copy/{resource}/{id} aimeos_shop_jqadm_copy › Aimeos\Shop › JqadmController@co…
  GET|POST|HEAD store-admin/{site}/jqadm/create/{resource} aimeos_shop_jqadm_create › Aimeos\Shop › JqadmController@cre…
  POST      store-admin/{site}/jqadm/delete/{resource}/{id?} aimeos_shop_jqadm_delete › Aimeos\Shop › JqadmController@d…
  GET|POST|HEAD store-admin/{site}/jqadm/export/{resource} aimeos_shop_jqadm_export › Aimeos\Shop › JqadmController@exp…
  GET|HEAD  store-admin/{site}/jqadm/file/{type} ..... aimeos_shop_jqadm_file › Aimeos\Shop › JqadmController@fileAction
  GET|HEAD  store-admin/{site}/jqadm/get/{resource}/{id} aimeos_shop_jqadm_get › Aimeos\Shop › JqadmController@getAction
  POST      store-admin/{site}/jqadm/import/{resource} aimeos_shop_jqadm_import › Aimeos\Shop › JqadmController@importA…
  POST      store-admin/{site}/jqadm/save/{resource} . aimeos_shop_jqadm_save › Aimeos\Shop › JqadmController@saveAction
  GET|POST|HEAD store-admin/{site}/jqadm/search/{resource} aimeos_shop_jqadm_search › Aimeos\Shop › JqadmController@sea…
  OPTIONS   store-admin/{site}/jsonadm/{resource?} aimeos_shop_jsonadm_options › Aimeos\Shop › JsonadmController@option…
  DELETE    store-admin/{site}/jsonadm/{resource}/{id?} aimeos_shop_jsonadm_delete › Aimeos\Shop › JsonadmController@de…
  GET|HEAD  store-admin/{site}/jsonadm/{resource}/{id?} aimeos_shop_jsonadm_get › Aimeos\Shop › JsonadmController@getAc…
  PATCH     store-admin/{site}/jsonadm/{resource}/{id?} aimeos_shop_jsonadm_patch › Aimeos\Shop › JsonadmController@pat…
  POST      store-admin/{site}/jsonadm/{resource}/{id?} aimeos_shop_jsonadm_post › Aimeos\Shop › JsonadmController@post…
  PUT       store-admin/{site}/jsonadm/{resource}/{id?} aimeos_shop_jsonadm_put › Aimeos\Shop › JsonadmController@putAc…
  GET|OPTIONS|HEAD store-jsonapi/{resource?} aimeos_shop_jsonapi_options › Aimeos\Shop › JsonapiController@optionsAction
  DELETE    store-jsonapi/{resource} ......... aimeos_shop_jsonapi_delete › Aimeos\Shop › JsonapiController@deleteAction
  GET|HEAD  store-jsonapi/{resource} ............... aimeos_shop_jsonapi_get › Aimeos\Shop › JsonapiController@getAction
  PATCH     store-jsonapi/{resource} ........... aimeos_shop_jsonapi_patch › Aimeos\Shop › JsonapiController@patchAction
  POST      store-jsonapi/{resource} ............. aimeos_shop_jsonapi_post › Aimeos\Shop › JsonapiController@postAction
  PUT       store-jsonapi/{resource} ............... aimeos_shop_jsonapi_put › Aimeos\Shop › JsonapiController@putAction

Shop routes in config/shop load these

'routes' => [
        // Docs: https://aimeos.org/docs/latest/laravel/extend/#custom-routes
        // Multi-sites: https://aimeos.org/docs/latest/laravel/customize/#multiple-shops
        // 'admin' => ['prefix' => 'admin', 'middleware' => ['web']],
        'admin' => ['prefix' => 'store-admin', 'middleware' => ['auth', 'web']],
        // 'jqadm' => ['prefix' => 'admin/{site}/jqadm', 'middleware' => ['web', 'auth']],
        'jqadm' => ['prefix' => 'store-admin/{site}/jqadm', 'middleware' => ['web', 'auth']],
        // 'graphql' => ['prefix' => 'admin/{site}/graphql', 'middleware' => ['web', 'auth']],
        'graphql' => false,
        // 'jsonadm' => ['prefix' => 'admin/{site}/jsonadm', 'middleware' => ['web', 'auth']],
        // 'jsonapi' => ['prefix' => 'jsonapi', 'middleware' => ['web', 'api']],
        'jsonadm' => ['prefix' => 'store-admin/{site}/jsonadm', 'middleware' => ['web', 'auth']],
        'jsonapi' => ['prefix' => 'store-jsonapi', 'middleware' => ['web', 'api']],
        // 'account' => ['prefix' => 'profile', 'middleware' => ['web', 'auth']],
        // 'default' => ['prefix' => 'shop', 'middleware' => ['web']],
        'default' => false,
        // 'confirm' => ['prefix' => 'shop', 'middleware' => ['web']],
        'confirm' => false,
        // 'supplier' => ['prefix' => 's', 'middleware' => ['web']],
        // 'page' => ['prefix' => 'p', 'middleware' => ['web']],
        // 'home' => ['middleware' => ['web']],
        'home' => false,
        // 'update' => [],
    ],

routes/web.php loads one Aimeos route to as I wanted to move them over to my routes and work on them from there

...
/*
|--------------------------------------------------------------------------
| Main Application domain routes including homes controller
|--------------------------------------------------------------------------
*/

Route::domain(config('site.app_domain'))->group(function () {
....
/*
    |--------------------------------------------------------------------------
    | Dashboard Routes
    |--------------------------------------------------------------------------
    */
    Route::prefix('dashboard')->middleware('auth', 'log.route')->group(function () {
        Route::get('store-admin', [Aimeos\Controller\AdminController::class, 'indexAction']);

so I have

GET|HEAD  smtv.test/dashboard/store-admin .................................. Aimeos\Shop › AdminController@indexAction
GET|HEAD  store-admin .................................. aimeos_shop_admin › Aimeos\Shop › AdminController@indexAction

And both of these routes show

[2024-06-06 06:52:01] local.INFO: Handling route: dashboard

for logging so authentication kicks in , user is sent to login and then back to dashboard. I need these routes to work. I need shop admin view to load. In shop.php I already have the admins from main app and database:

'roles' => ['admin', 'editor, smart-administrator'.'administrator'], // user groups allowed to access the admin backend

But perhaps because Aimeos and main app are in different databases the roles do not load nor validate?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To integrate Aimeos into your existing Laravel application with a separate database for Aimeos, you need to ensure that the authentication and user roles are correctly configured to work across both databases. Here’s a step-by-step solution to address the issues you’re facing:

Step 1: Configure Database Connections

Ensure that your config/database.php has the correct configuration for both databases. For example:

'connections' => [
    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        // other settings...
    ],
    'aimeos' => [
        'driver' => 'mysql',
        'host' => env('AIMEOS_DB_HOST', '127.0.0.1'),
        'database' => env('AIMEOS_DB_DATABASE', 'aimeos'),
        'username' => env('AIMEOS_DB_USERNAME', 'aimeos'),
        'password' => env('AIMEOS_DB_PASSWORD', ''),
        // other settings...
    ],
],

Step 2: Set Up Models to Use the Correct Database Connection

Ensure that the models used by Aimeos are configured to use the aimeos database connection. For example, if you have a User model for Aimeos, it should look like this:

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class AimeosUser extends Authenticatable
{
    protected $connection = 'aimeos';
    protected $table = 'users'; // or whatever the table name is in the Aimeos database
}

Step 3: Configure Authentication Guards

Update your config/auth.php to include a guard for Aimeos:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'aimeos' => [
        'driver' => 'session',
        'provider' => 'aimeos_users',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'aimeos_users' => [
        'driver' => 'eloquent',
        'model' => App\Models\AimeosUser::class,
    ],
],

Step 4: Middleware for Authentication

Ensure that your routes are using the correct middleware for authentication. You might need to create a custom middleware if you need to check roles across different databases.

Step 5: Update Aimeos Configuration

In your config/shop.php, ensure that the routes are correctly set up to use the auth middleware:

'routes' => [
    'admin' => ['prefix' => 'store-admin', 'middleware' => ['auth:aimeos', 'web']],
    'jqadm' => ['prefix' => 'store-admin/{site}/jqadm', 'middleware' => ['auth:aimeos', 'web']],
    'jsonadm' => ['prefix' => 'store-admin/{site}/jsonadm', 'middleware' => ['auth:aimeos', 'web']],
    'jsonapi' => ['prefix' => 'store-jsonapi', 'middleware' => ['web', 'api']],
    'default' => false,
    'confirm' => false,
    'home' => false,
],

Step 6: Ensure Roles and Permissions

Make sure that the roles and permissions are correctly set up in the Aimeos database. You might need to synchronize roles between your main app and Aimeos.

Step 7: Testing

Test the routes to ensure that the authentication works as expected. You can use Laravel’s built-in logging to debug any issues:

Route::middleware(['auth:aimeos', 'web'])->group(function () {
    Route::get('store-admin', [Aimeos\Controller\AdminController::class, 'indexAction']);
});

Conclusion

By following these steps, you should be able to integrate Aimeos into your existing Laravel application with a separate database for Aimeos, ensuring that authentication and user roles work correctly. If you encounter any specific issues, you can enable detailed logging to help debug the problem.

1 like

Please or to participate in this conversation.