PHP code example of libinkk / oneauth

1. Go to this page and download the library: Download libinkk/oneauth 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/ */

    

libinkk / oneauth example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Libinkk\OneAuth\Traits\HasOneAuth;

class User extends Authenticatable
{
    use HasOneAuth;

    protected $fillable = [
        'name',
        'email',
        'username',
        'phone',
        'password',
        'email_verified_at',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

$user->oneauthSessions();
$user->oneauthDevices();
$user->oneauthSocialAccounts();

'identifier_fields' => ['email', 'username', 'phone'],

'routes' => [
    'enabled' => true,
    'prefix' => 'oneauth',
    'middleware' => ['web'],
],

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
}

use Libinkk\OneAuth\Facades\OneAuth;

$result = OneAuth::register([
    'name' => 'Taylor Doe',
    'email' => '[email protected]',
    'password' => 'Password123',
]);

$user = $result['user'];

$result = OneAuth::login([
    'email' => '[email protected]',
    'password' => 'Password123',
]);

$result = OneAuth::login([
    'username' => 'taylor',
    'password' => 'Password123',
]);

$result = OneAuth::login([
    'phone' => '+15551234567',
    'password' => 'Password123',
]);

$result = OneAuth::login([
    'identifier' => '[email protected]',
    'password' => 'Password123',
]);

[
    'user' => $user,
    'token' => $accessTokenOrNull,
    'refresh_token' => $refreshTokenOrNull,
]

OneAuth::sendOtp([
    'email' => '[email protected]',
    'purpose' => 'login',
    'channel' => 'email',
    'target' => '[email protected]',
]);

$result = OneAuth::loginWithOtp([
    'email' => '[email protected]',
    'target' => '[email protected]',
    'code' => '123456',
]);

$verified = OneAuth::verifyOtp([
    'email' => '[email protected]',
    'purpose' => 'login',
    'target' => '[email protected]',
    'code' => '123456',
]);

$result = OneAuth::anonymousLogin();

$user = OneAuth::user();

OneAuth::logout();

$result = OneAuth::refresh();

OneAuth::sendEmailVerification(['email' => '[email protected]']);

$verified = OneAuth::verifyEmail([
    'email' => '[email protected]',
    'token' => $tokenFromEmail,
]);

OneAuth::forgotPassword(['email' => '[email protected]']);

OneAuth::resetPassword([
    'email' => '[email protected]',
    'token' => $brokerToken,
    'password' => 'NewPassword123',
]);

OneAuth::changePassword([
    'current_password' => 'Password123',
    'new_password' => 'NewPassword123',
]);

$setup = OneAuth::enableTwoFactor(['method' => 'totp']); // or email / sms
OneAuth::verifyTwoFactor(['code' => $code]);
OneAuth::disableTwoFactor(['password' => $currentPassword]);

try {
    $result = OneAuth::login($credentials);
} catch (\Libinkk\OneAuth\Exceptions\TwoFactorRequiredException $e) {
    $result = OneAuth::completeTwoFactorLogin([
        'challenge_token' => $e->getChallengeToken(),
        'code' => $totpOrEmailCode,
    ]);
}

$result = OneAuth::socialLogin('google', [
    'access_token' => $providerAccessToken,
]);

$sessions = OneAuth::sessions();
OneAuth::revokeSession($sessionId);
OneAuth::logoutOtherSessions();

$devices = OneAuth::devices();
OneAuth::trustDevice($fingerprint, true);

OneAuth::lockAccount('[email protected]', 600, 'manual');
OneAuth::unlockAccount('[email protected]');

$jwt = OneAuth::driver('jwt');
$result = $jwt->login([
    'email' => '[email protected]',
    'password' => 'Password123',
]);

$challenge = OneAuth::sendOtp([
    'email' => '[email protected]',
    'purpose' => 'login',
    'channel' => 'email',
    'target' => '[email protected]',
]);

$verified = OneAuth::verifyOtp([
    'email' => '[email protected]',
    'purpose' => 'login',
    'target' => '[email protected]',
    'code' => '123456',
]);

$result = OneAuth::loginWithOtp([
    'email' => '[email protected]',
    'target' => '[email protected]',
    'code' => '123456',
]);



namespace App\Auth;

use Libinkk\OneAuth\Contracts\OTPProviderInterface;

class TwilioOtpProvider implements OTPProviderInterface
{
    public function send(
        string $channel,
        string $to,
        string $code,
        array $context = []
    ): void {
        // Send the code through your provider.
    }
}

use App\Auth\TwilioOtpProvider;
use Libinkk\OneAuth\Contracts\OTPProviderInterface;

public function register(): void
{
    $this->app->bind(OTPProviderInterface::class, TwilioOtpProvider::class);
}

$result = OneAuth::sendEmailVerification([
    'email' => '[email protected]',
]);

[
    'id' => 1,
    'expires_at' => $date,
    'signed_url' => $url,
]

$verified = OneAuth::verifyEmail([
    'email' => '[email protected]',
    'token' => 'token-from-email',
]);

OneAuth::forgotPassword(['email' => '[email protected]']);
OneAuth::resetPassword([
    'email' => '[email protected]',
    'token' => $brokerToken,
    'password' => 'NewPassword123',
]);
OneAuth::changePassword([
    'current_password' => 'Password123',
    'new_password' => 'NewPassword123',
]);

$setup = OneAuth::enableTwoFactor([
    'method' => 'totp',
]);

$setup = OneAuth::enableTwoFactor([
    'method' => 'email',
]);

[
    'method' => 'totp',
    'secret' => 'BASE32SECRET',
    'recovery_codes' => [
        'RECOVERY01',
        'RECOVERY02',
    ],
    'confirmed' => false,
    'otpauth_uri' => 'otpauth://totp/OneAuth:[email protected]?secret=...',
    'issuer' => 'OneAuth',
]

$verified = OneAuth::verifyTwoFactor([
    'code' => '123456',
]);

$verified = OneAuth::verifyTwoFactor([
    'recovery_code' => 'RECOVERY01',
]);

OneAuth::disableTwoFactor([
    'password' => $currentPassword,
]);

try {
    $result = OneAuth::login($credentials);
} catch (\Libinkk\OneAuth\Exceptions\TwoFactorRequiredException $e) {
    $result = OneAuth::completeTwoFactorLogin([
        'challenge_token' => $e->getChallengeToken(),
        'code' => $totpCode,
    ]);
}

'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => env('GOOGLE_REDIRECT_URI'),
],

$result = OneAuth::socialLogin('google', [
    'token' => $providerAccessToken,
]);

$sessions = OneAuth::sessions();

$devices = OneAuth::devices();

OneAuth::trustDevice($fingerprint, true);

OneAuth::revokeSession($sessionId);
OneAuth::logoutOtherSessions();

$result = OneAuth::anonymousLogin();

OneAuth::lockAccount('[email protected]', 600, 'manual');
OneAuth::unlockAccount('[email protected]');

Route::middleware(['oneauth.auth', 'oneauth.verified'])->group(function () {
    Route::get('/account', AccountController::class);
});

use Libinkk\OneAuth\Events\UserLoggedIn;

class RecordLoginAnalytics
{
    public function handle(UserLoggedIn $event): void
    {
        $user = $event->user;
    }
}

$event->user;
$event->context;

Libinkk\OneAuth\Contracts\AuthenticationDriverInterface
Libinkk\OneAuth\Contracts\OTPProviderInterface
Libinkk\OneAuth\Contracts\OAuthProviderInterface
Libinkk\OneAuth\Contracts\NotificationProviderInterface
Libinkk\OneAuth\Contracts\SessionRepositoryInterface
Libinkk\OneAuth\Contracts\DeviceRepositoryInterface

use App\Auth\DatabaseSessionRepository;
use Libinkk\OneAuth\Contracts\SessionRepositoryInterface;

public function register(): void
{
    $this->app->bind(
        SessionRepositoryInterface::class,
        DatabaseSessionRepository::class
    );
}

interface AuthenticationDriverInterface
{
    public function attempt(array $credentials): mixed;
    public function establish(mixed $user): array;
    public function login(array $credentials): array;
    public function logout(): void;
    public function refresh(): array;
    public function user(): mixed;
    public function check(): bool;
    public function guest(): bool;
    public function token(): ?string;
}

use Illuminate\Support\Facades\Schedule;

Schedule::command('oneauth:cleanup')->hourly();

protected function schedule(Schedule $schedule): void
{
    $schedule->command('oneauth:cleanup')->hourly();
}
bash
php artisan oneauth:install
bash
php artisan migrate
bash
php artisan oneauth:install --migrate
bash
php artisan oneauth:install --force
php artisan oneauth:publish --force
bash
php artisan oneauth:doctor
bash
composer vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
bash
php -r "echo bin2hex(random_bytes(32)), PHP_EOL;"
bash
php artisan oneauth:publish
bash
php artisan oneauth:install
php artisan oneauth:install --migrate
php artisan oneauth:install --force
bash
php artisan oneauth:publish
php artisan oneauth:publish --force
bash
php artisan oneauth:doctor
bash
php artisan oneauth:cleanup
bash
php artisan oneauth:doctor
php artisan migrate
bash
php artisan oneauth:publish
php artisan migrate
php artisan oneauth:doctor