add single sign on with WHMCS

This commit is contained in:
Bozhidar 2024-05-13 00:58:50 +03:00
parent 6b2263a8c9
commit 2ee8605efb
4 changed files with 60 additions and 1 deletions

View file

@ -6,6 +6,9 @@ use App\Http\Controllers\Api\Request\CustomerCreateRequest;
use App\Http\Controllers\ApiController; use App\Http\Controllers\ApiController;
use App\Models\Customer; use App\Models\Customer;
use App\Models\HostingSubscription; use App\Models\HostingSubscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\PersonalAccessToken;
class CustomersController extends ApiController class CustomersController extends ApiController
{ {
@ -130,4 +133,51 @@ class CustomersController extends ApiController
], ],
]); ]);
} }
public function loginWithToken($customerId, Request $request)
{
$findCustomer = Customer::where('id', $customerId)->first();
if (!$findCustomer) {
return response()->json([
'status' => 'error',
'message' => 'Customer not found',
], 404);
}
$findToken = $findCustomer->tokens()->where('token', $request->token)->where('name', 'externalLogin')->first();
if (!$findToken) {
return response()->json([
'status' => 'error',
'message' => 'Token not found',
], 404);
}
Auth::guard('web_customer')->loginUsingId($findCustomer->id);
return redirect('/customer');
}
public function generateLoginToken($customerId, Request $request)
{
$findCustomer = Customer::where('id', $customerId)->first();
if (! $findCustomer) {
return response()->json([
'status' => 'error',
'message' => 'Customer not found',
], 404);
}
$findCustomer->tokens()->delete();
$token = $findCustomer->createToken('externalLogin',['*'], now()->addMinute());
return response()->json([
'status' => 'ok',
'message' => 'Token generated',
'data' => [
'token' => $token->accessToken->token,
],
]);
}
} }

View file

@ -5,9 +5,11 @@ namespace App\Models;
use App\ApiSDK\PhyreApiSDK; use App\ApiSDK\PhyreApiSDK;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
class Customer extends Authenticatable class Customer extends Authenticatable
{ {
use HasApiTokens;
use HasFactory; use HasFactory;
protected $fillable = [ protected $fillable = [

View file

@ -24,7 +24,11 @@ Route::middleware(\App\Http\Middleware\ApiKeyMiddleware::class)->group(function(
Route::get('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'show'])->name('api.customers.show'); Route::get('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'show'])->name('api.customers.show');
Route::put('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'update'])->name('api.customers.update'); Route::put('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'update'])->name('api.customers.update');
Route::delete('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'destroy'])->name('api.customers.destroy'); Route::delete('customers/{id}', [\App\Http\Controllers\Api\CustomersController::class, 'destroy'])->name('api.customers.destroy');
Route::get('customers/{id}/hosting-subscriptions', [\App\Http\Controllers\Api\CustomersController::class, 'getHostingSubscriptionsByCustomerId'])->name('api.customers.hosting-subscriptions'); Route::get('customers/{id}/hosting-subscriptions', [\App\Http\Controllers\Api\CustomersController::class, 'getHostingSubscriptionsByCustomerId'])
->name('api.customers.hosting-subscriptions');
Route::get('/customers/{id}/generate-login-token', [\App\Http\Controllers\Api\CustomersController::class, 'generateLoginToken'])
->name('api.customers.generate-login-token');
// Hosting subscriptions // Hosting subscriptions
Route::get('hosting-subscriptions', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'index'])->name('api.hosting-subscriptions.index'); Route::get('hosting-subscriptions', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'index'])->name('api.hosting-subscriptions.index');

View file

@ -31,3 +31,6 @@ Route::get('/installer', \App\Livewire\Installer::class);
Route::get('backup/download', [\App\Http\Controllers\BackupDownloadController::class, 'download']) Route::get('backup/download', [\App\Http\Controllers\BackupDownloadController::class, 'download'])
->name('backup.download'); ->name('backup.download');
Route::get('/customers/{id}/login-with-token', [\App\Http\Controllers\Api\CustomersController::class, 'loginWithToken'])
->name('customers.login-with-token');