PHP code example of zhylon / zhylon-auth
1. Go to this page and download the library: Download zhylon/zhylon-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/ */
zhylon / zhylon-auth example snippets
// app/Models/User.php
protected $fillable = [
'name',
'email',
// ... your existing fields ...
'zhylon_id',
'zhylon_token',
'zhylon_refresh_token',
];
namespace App\Http\Controllers\Auth;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
class ZhylonAuthController extends Controller
{
/**
* Redirect the user to the ZhylonID authorization page.
*/
public function redirect()
{
return Socialite::driver('zhylon')
->scopes(['profile.read'])
->redirect();
}
/**
* Handle the callback from ZhylonID after authorization.
*/
public function callback()
{
$socialiteUser = Socialite::driver('zhylon')->user();
$user = User::updateOrCreate(
['zhylon_id' => $socialiteUser->getId()],
[
'name' => $socialiteUser->getName(),
'email' => $socialiteUser->getEmail(),
'zhylon_token' => $socialiteUser->token,
'zhylon_refresh_token' => $socialiteUser->refreshToken,
]
);
Auth::login($user, remember: true);
return redirect()->intended(config('zhylon-auth.home', '/dashboard'));
}
}
use App\Http\Controllers\Auth\ZhylonAuthController;
Route::get('/auth/zhylon', [ZhylonAuthController::class, 'redirect'])->name('auth.zhylon');
Route::get('/auth/zhylon/callback', [ZhylonAuthController::class, 'callback'])->name('auth.zhylon.callback');
$user = Auth::user();
echo $user->zhylon_id; // Zhylon user identifier
echo $user->zhylon_token; // Current access token
echo $user->zhylon_refresh_token; // Refresh token
use Laravel\Socialite\Facades\Socialite;
$newToken = Socialite::driver('zhylon')
->refreshToken($user->zhylon_refresh_token);
$user->update([
'zhylon_token' => $newToken->token,
'zhylon_refresh_token' => $newToken->refreshToken,
]);
bash
php artisan vendor:publish --provider="Zhylon\ZhylonAuth\ZhylonAuthServiceProvider"
bash
php artisan migrate