Skip to content

Commit

Permalink
add single sign on with WHMCS
Browse files Browse the repository at this point in the history
  • Loading branch information
bobicloudvision committed May 12, 2024
1 parent 6b2263a commit 2ee8605
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
50 changes: 50 additions & 0 deletions web/app/Http/Controllers/Api/CustomersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use App\Http\Controllers\ApiController;
use App\Models\Customer;
use App\Models\HostingSubscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\PersonalAccessToken;

class CustomersController extends ApiController
{
Expand Down Expand Up @@ -130,4 +133,51 @@ public function getHostingSubscriptionsByCustomerId($customerId)
],
]);
}

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,
],
]);

}
}
2 changes: 2 additions & 0 deletions web/app/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\ApiSDK\PhyreApiSDK;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;

class Customer extends Authenticatable
{
use HasApiTokens;
use HasFactory;

protected $fillable = [
Expand Down
6 changes: 5 additions & 1 deletion web/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
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::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
Route::get('hosting-subscriptions', [\App\Http\Controllers\Api\HostingSubscriptionsController::class, 'index'])->name('api.hosting-subscriptions.index');
Expand Down
3 changes: 3 additions & 0 deletions web/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@

Route::get('backup/download', [\App\Http\Controllers\BackupDownloadController::class, 'download'])
->name('backup.download');

Route::get('/customers/{id}/login-with-token', [\App\Http\Controllers\Api\CustomersController::class, 'loginWithToken'])
->name('customers.login-with-token');

0 comments on commit 2ee8605

Please sign in to comment.