PHP code example of ghostcompiler / laravel-auth
1. Go to this page and download the library: Download ghostcompiler/laravel-auth library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ghostcompiler / laravel-auth example snippets
use Illuminate\Support\Facades\Route;
Route::middleware(['auth', 'laravel-auth.2fa'])->group(function () {
Route::get('/billing', fn () => 'protected');
Route::get('/settings/security', fn () => 'security');
});
Route::post('/security/otp/verify', [SecurityController::class, 'verifyOtp'])
->middleware(['auth', 'laravel-auth.throttle:otp']);
Route::post('/security/passkey/verify', [SecurityController::class, 'verifyPasskey'])
->middleware(['auth', 'laravel-auth.throttle:passkey']);
$setup = LaravelAuth::enable2FA(auth()->user());
return response()->json([
'secret' => $setup['secret'],
'otpauth_uri' => $setup['otpauth_uri'],
]);
$result = LaravelAuth::confirmTwoFactorSetup(
auth()->user(),
$request->string('code')
);
return response()->json([
'recovery_codes' => $result['recovery_codes'],
]);
LaravelAuth::disable2FA(auth()->user());
namespace App\Http\Controllers\Security;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use LaravelAuth;
class TwoFactorController extends Controller
{
public function begin(Request $request)
{
$setup = LaravelAuth::enable2FA($request->user());
return response()->json($setup);
}
public function confirm(Request $request)
{
$result = LaravelAuth::confirmTwoFactorSetup(
$request->user(),
$request->string('code')
);
return response()->json($result);
}
public function disable(Request $request)
{
LaravelAuth::disable2FA($request->user());
return response()->json(['status' => 'disabled']);
}
}
$ok = LaravelAuth::attemptOtp(
$user,
$request->string('code'),
rememberDevice: (bool) $request->boolean('remember_device'),
deviceName: $request->string('device_name')->toString() ?: null,
);
abort_unless($ok, 422, 'Invalid code.');
$proof = LaravelAuth::verifyOTP($user, $request->string('code'));
$codes = LaravelAuth::generateRecoveryCodes(auth()->user());
$options = LaravelAuth::registerPasskey(auth()->user(), 'MacBook Pro');
$passkey = LaravelAuth::finishPasskeyRegistration(
auth()->user(),
$request->all(),
'MacBook Pro'
);
$options = LaravelAuth::requestPasskeyAssertion(auth()->user());
$proof = LaravelAuth::verifyPasskeyAssertion(auth()->user(), $request->all());
$ok = LaravelAuth::attemptPasskey(
auth()->user(),
$request->all(),
rememberDevice: true,
deviceName: 'Office Laptop'
);
LaravelAuth::sendEmailOtp(auth()->user());
$proof = LaravelAuth::verifyEmailOtp(auth()->user(), $request->string('code'));
$ok = LaravelAuth::attemptEmailOtp(auth()->user(), $request->string('code'));
LaravelAuth::sendSmsOtp(auth()->user(), '+15550001111');
$proof = LaravelAuth::verifySmsOtp(
auth()->user(),
'+15550001111',
$request->string('code')
);
$ok = LaravelAuth::attemptSmsOtp(
auth()->user(),
'+15550001111',
$request->string('code')
);
LaravelAuth::sendWhatsAppOtp(auth()->user(), '+15550002222');
$proof = LaravelAuth::verifyWhatsAppOtp(
auth()->user(),
'+15550002222',
$request->string('code')
);
$ok = LaravelAuth::attemptWhatsAppOtp(
auth()->user(),
'+15550002222',
$request->string('code')
);
namespace App\Http\Controllers\Security;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use LaravelAuth;
class OtpController extends Controller
{
public function sendEmail(Request $request)
{
return response()->json(
LaravelAuth::sendEmailOtp($request->user())
);
}
public function verifyEmail(Request $request)
{
$ok = LaravelAuth::attemptEmailOtp(
$request->user(),
$request->string('code')
);
abort_unless($ok, 422, 'Invalid email OTP.');
return response()->json(['status' => 'verified']);
}
public function sendSms(Request $request)
{
return response()->json(
LaravelAuth::sendSmsOtp($request->user(), $request->string('phone'))
);
}
public function verifySms(Request $request)
{
$ok = LaravelAuth::attemptSmsOtp(
$request->user(),
$request->string('phone'),
$request->string('code')
);
abort_unless($ok, 422, 'Invalid SMS OTP.');
return response()->json(['status' => 'verified']);
}
}
'social' => [
'default_stateless' => false,
'providers' => [
'google' => [
'enabled' => true,
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
'scopes' => ['openid', 'profile', 'email'],
'with' => [],
],
'github' => [
'enabled' => true,
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI'),
'scopes' => ['read:user', 'user:email'],
'with' => [],
],
],
],
$providers = LaravelAuth::socialProviders();
return LaravelAuth::redirectToSocialProvider('google');
$profile = LaravelAuth::resolveSocialUser('google');
$linked = LaravelAuth::syncSocialAccount(auth()->user(), 'google', $profile);
$user = LaravelAuth::findUserBySocialAccount('google', $profile);
$accounts = LaravelAuth::linkedSocialAccounts(auth()->user());
LaravelAuth::unlinkSocialAccount(auth()->user(), 'google');
namespace App\Http\Controllers\Auth;
use Illuminate\Routing\Controller;
use LaravelAuth;
class SocialAuthController extends Controller
{
public function redirect(string $provider)
{
return LaravelAuth::redirectToSocialProvider($provider);
}
public function callback(string $provider)
{
$profile = LaravelAuth::resolveSocialUser($provider);
$user = LaravelAuth::findUserBySocialAccount($provider, $profile);
if (! $user) {
return redirect('/login')->withErrors([
'email' => 'No linked account found.',
]);
}
auth()->login($user);
return redirect('/dashboard');
}
public function connect(string $provider)
{
return LaravelAuth::redirectToSocialProvider($provider);
}
public function connectCallback(string $provider)
{
$profile = LaravelAuth::resolveSocialUser($provider);
LaravelAuth::syncSocialAccount(auth()->user(), $provider, $profile);
return redirect('/settings/connections')->with('status', 'Provider linked.');
}
}
$providers = LaravelAuth::socialProviders([
'google' => [
'client_id' => $tenant->google_client_id,
'client_secret' => $tenant->google_client_secret,
'redirect' => route('tenant.social.callback', ['provider' => 'google']),
],
'github' => [
'client_id' => $tenant->github_client_id,
'client_secret' => $tenant->github_client_secret,
'redirect' => route('tenant.social.callback', ['provider' => 'github']),
],
]);
return LaravelAuth::redirectToSocialProvider(
'google',
runtimeConfig: [
'client_id' => $tenant->google_client_id,
'client_secret' => $tenant->google_client_secret,
'redirect' => route('tenant.social.callback', ['provider' => 'google']),
'scopes' => ['openid', 'profile', 'email'],
'stateless' => true,
],
);
$config = [
'client_id' => $tenant->google_client_id,
'client_secret' => $tenant->google_client_secret,
'redirect' => route('tenant.social.callback', ['provider' => 'google']),
'scopes' => ['openid', 'profile', 'email'],
'stateless' => true,
];
return LaravelAuth::redirectToSocialProvider('google', [], [], null, $config);
$profile = LaravelAuth::resolveSocialUser(
'github',
runtimeConfig: [
'client_id' => $tenant->github_client_id,
'client_secret' => $tenant->github_client_secret,
'redirect' => route('tenant.social.callback', ['provider' => 'github']),
'stateless' => true,
],
);
$profile = LaravelAuth::resolveSocialUser('github', null, $config);
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use LaravelAuth;
class TenantSocialAuthController extends Controller
{
public function redirect(Request $request, string $provider)
{
$config = $this->runtimeConfig($request, $provider);
return LaravelAuth::redirectToSocialProvider($provider, [], [], null, $config);
}
public function callback(Request $request, string $provider)
{
$config = $this->runtimeConfig($request, $provider);
$profile = LaravelAuth::resolveSocialUser($provider, null, $config);
$user = LaravelAuth::findUserBySocialAccount($provider, $profile);
if (! $user) {
return redirect('/login')->withErrors([
'email' => 'No linked account found for this social login.',
]);
}
auth()->login($user);
return redirect('/dashboard');
}
public function connect(Request $request, string $provider)
{
$config = $this->runtimeConfig($request, $provider);
return LaravelAuth::redirectToSocialProvider($provider, [], [], null, $config);
}
public function connectCallback(Request $request, string $provider)
{
$config = $this->runtimeConfig($request, $provider);
$profile = LaravelAuth::resolveSocialUser($provider, null, $config);
LaravelAuth::syncSocialAccount($request->user(), $provider, $profile);
return redirect('/settings/connections')->with('status', 'Provider linked.');
}
private function runtimeConfig(Request $request, string $provider): array
{
$tenant = tenant();
$oauth = $tenant->oauthProviders()->where('provider', $provider)->first();
abort_unless($oauth, 404, 'Provider not configured for this tenant.');
return [
'client_id' => $oauth->client_id,
'client_secret' => $oauth->client_secret,
'redirect' => route('tenant.social.callback', ['provider' => $provider]),
'scopes' => $this->defaultScopes($provider),
'stateless' => true,
];
}
private function defaultScopes(string $provider): array
{
return match ($provider) {
'google' => ['openid', 'profile', 'email'],
'github' => ['read:user', 'user:email'],
default => [],
};
}
}
use App\Http\Controllers\Auth\TenantSocialAuthController;
use Illuminate\Support\Facades\Route;
Route::get('/auth/{provider}', [TenantSocialAuthController::class, 'redirect'])
->name('tenant.social.redirect');
Route::get('/auth/{provider}/callback', [TenantSocialAuthController::class, 'callback'])
->name('tenant.social.callback');
Route::middleware('auth')->group(function () {
Route::get('/settings/connections/{provider}', [TenantSocialAuthController::class, 'connect'])
->name('tenant.social.connect');
Route::get('/settings/connections/{provider}/callback', [TenantSocialAuthController::class, 'connectCallback'])
->name('tenant.social.connect.callback');
});
$state = LaravelAuth::state($user = null);
$verified = LaravelAuth::isVerified($user = null);
$full = LaravelAuth::isFullyAuthenticated($user = null);
$pending = LaravelAuth::isPending2FA($user = null);
$
LaravelAuth::enforce($user = null);
LaravelAuth::throttle('otp', $user = null);
LaravelAuth::tooManyAttempts('otp', $user = null);
LaravelAuth::clearThrottle('otp', $user = null);
LaravelAuth::preset('strict');
'otp_channels' => [
'sms' => [
'provider' => env('LARAVEL_AUTH_SMS_PROVIDER', 'twilio'),
],
],
'sms' => [
'provider' => 'custom',
'custom_transport' => App\LaravelAuth\SmsOtpTransport::class,
],
'whatsapp' => [
'enabled' => true,
'provider' => 'custom',
'custom_transport' => App\LaravelAuth\WhatsAppOtpTransport::class,
],
LaravelAuth::redirectToSocialProvider($provider, [], [], null, $config);
LaravelAuth::resolveSocialUser($provider, null, $config);
LaravelAuth::enable2FA($user);
LaravelAuth::confirmTwoFactorSetup($user, $code);
LaravelAuth::disable2FA($user);
LaravelAuth::verifyOTP($user, $code);
LaravelAuth::generateRecoveryCodes($user);
LaravelAuth::registerPasskey($user, $name = null);
LaravelAuth::finishPasskeyRegistration($user, $payload, $name = null);
LaravelAuth::requestPasskeyAssertion($user);
LaravelAuth::verifyPasskeyAssertion($user, $payload);
LaravelAuth::attemptPasskey($user, $payload, $rememberDevice = false, $deviceName = null);
LaravelAuth::sendEmailOtp($user, $email = null, $context = []);
LaravelAuth::verifyEmailOtp($user, $code, $email = null, $context = []);
LaravelAuth::attemptEmailOtp($user, $code, $email = null, $context = []);
LaravelAuth::sendSmsOtp($user, $phoneNumber, $context = []);
LaravelAuth::verifySmsOtp($user, $phoneNumber, $code, $context = []);
LaravelAuth::attemptSmsOtp($user, $phoneNumber, $code, $context = []);
LaravelAuth::sendWhatsAppOtp($user, $phoneNumber, $context = []);
LaravelAuth::verifyWhatsAppOtp($user, $phoneNumber, $code, $context = []);
LaravelAuth::attemptWhatsAppOtp($user, $phoneNumber, $code, $context = []);
LaravelAuth::socialProviders($runtimeProviders = []);
LaravelAuth::redirectToSocialProvider($provider, $scopes = [], $with = [], $stateless = null, $runtimeConfig = []);
LaravelAuth::resolveSocialUser($provider, $stateless = null, $runtimeConfig = []);
LaravelAuth::syncSocialAccount($user, $provider, $profile);
LaravelAuth::findUserBySocialAccount($provider, $socialIdentity);
LaravelAuth::linkedSocialAccounts($user);
LaravelAuth::unlinkSocialAccount($user, $provider, $providerUserId = null);
LaravelAuth::state($user = null);
LaravelAuth::isVerified($user = null);
LaravelAuth::isFullyAuthenticated($user = null);
LaravelAuth::isPending2FA($user = null);
LaravelAuth::enforce($user = null);
LaravelAuth::throttle('otp', $user = null);
LaravelAuth::tooManyAttempts('otp', $user = null);
LaravelAuth::clearThrottle('otp', $user = null);
LaravelAuth::preset('strict');
LaravelAuth::bash
composer ghost:laravel-auth
php artisan migrate
bash
php artisan ghost:laravel-auth --force
bash
php artisan laravel-auth:otp:publish
bash
composer ghost:laravel-auth
php artisan migrate
php artisan optimize:clear
bash
composer update ghostcompiler/laravel-auth
composer dump-autoload
php artisan optimize:clear
bash
php artisan laravel-auth:otp:publish
bash
composer update ghostcompiler/laravel-auth
composer dump-autoload
php artisan optimize:clear