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

aosdev's avatar

Sanctum with another database

Hey guys, i'm struggling with a problem and can't figure it out how can i make it possible.

There is a application named osticket and it has database inside itself. I created a new laravel application for these users able to create posts, i put a button on header and i'm redirecting from osticket to my own application like;

http://localhost/scp/workflow?user=f7177163c833dff4b38fc8d2872f1ec6

The "user" field in the url getting "id" from session and I'm getting this hashed "id" from my application. The thing is i want to check it from the "osticket" database for define which user coming in to my application. So i have to create some session or authentication with this id and i should store user info. So i can use them when user want to create a post, i can know which user came form osticket. I prefer use sanctum if i can, but the user info doesn't exist in my application. Maybe i can create user in first redirect with osticket db info, there is some info like "username, email, id" etc.

I can't plan how can i this, please help.

Kind regards.

0 likes
1 reply
LaryAI's avatar
Level 58

One solution could be to create a custom guard in Laravel that uses the osticket database to authenticate users. Here are the steps to implement this solution:

  1. Create a new guard in config/auth.php:
'guards' => [
    // ...
    'osticket' => [
        'driver' => 'session',
        'provider' => 'osticket',
    ],
],
  1. Create a new provider in config/auth.php:
'providers' => [
    // ...
    'osticket' => [
        'driver' => 'eloquent',
        'model' => App\Models\OsticketUser::class,
    ],
],
  1. Create a new model OsticketUser that extends Illuminate\Foundation\Auth\User and uses the osticket database connection:
namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class OsticketUser extends Authenticatable
{
    protected $connection = 'osticket';
    // ...
}
  1. Create a middleware that checks if the user is authenticated with the osticket guard and sets the user information in the session:
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AuthenticateWithOsticket
{
    public function handle($request, Closure $next)
    {
        if (Auth::guard('osticket')->check()) {
            $user = Auth::guard('osticket')->user();
            session(['osticket_user_id' => $user->id]);
            session(['osticket_user_name' => $user->name]);
            session(['osticket_user_email' => $user->email]);
        }

        return $next($request);
    }
}
  1. Register the middleware in app/Http/Kernel.php:
protected $middlewareGroups = [
    'web' => [
        // ...
        \App\Http\Middleware\AuthenticateWithOsticket::class,
    ],
    // ...
];
  1. Use the auth middleware in your routes to protect them:
Route::middleware('auth')->group(function () {
    // ...
});
  1. Use the Auth::user() method to get the authenticated user in your controllers:
$user = Auth::user();

With this solution, you can authenticate users with the osticket database and store their information in the session. You can then use this information to identify the user when they create a post.

1 like

Please or to participate in this conversation.