axm / socialite
Axm的OAuth
1.0.13
2024-03-13 13:29 UTC
Requires
- php: ^8.1
- axm/framework: ^1.0.9
Requires (Dev)
- phpunit/phpunit: ^9.6
README
📦 安装
要安装组件,请将以下内容添加到您的 composer.json 文件中
{
"require": {
"axm/socialite": "^1.0"
}
}
您也可以使用 Composer 快速在项目中安装Axm。
composer require axm/Socialite
然后,运行 composer update 安装软件包。
配置
一旦安装了软件包,您需要对其进行配置。打开 .env 文件并添加以下行
GOOGLE_CLIENT_ID="YOUR_GOOGLE_CLIENT_ID" GOOGLE_CLIENT_SECRET="YOUR_GOOGLE_CLIENT_SECRET" GOOGLE_REDIRECT="${APP_URL}/YOUR_GOOGLE_REDIRECT_URI" FACEBOOK_CLIENT_ID="YOUR_FACEBOOK_CLIENT_ID" FACEBOOK_CLIENT_SECRET="YOUR_FACEBOOK_CLIENT_SECRET" FACEBOOK_REDIRECT="${APP_URL}/YOUR_FACEBOOK_REDIRECT_URI"
请确保用您实际的Google和Facebook客户端ID、客户端密钥和重定向URI替换占位符。
用法
要使用此组件,只需将以下行添加到您的路由文件中
Route::get('/auth-redirect/{provider:\w+}', [App\Raxm\AuthComponent::class, 'handlerAuthRedirect']); Route::get('/auth-google-callback', [App\Raxm\AuthComponent::class, 'handlerRediretGoogleAuth']);
此路由将用户重定向到指定提供者的认证页面。
一旦用户完成认证,他们将重定向回您的应用程序。然后调用 handleSocialAuthRedirect 方法。此方法将检查用户是否已登录。如果他们已登录,他们将重定向到主页。如果他们未登录,他们将作为新用户注册,然后重定向到主页。
示例
以下示例展示了如何使用此组件通过Google和Facebook进行用户认证,用法在您的控制器中
<?php namespace App\Raxm; use Views\View; use App\Models\User; use Axm\Raxm\Component; use Axm\Socialite\Socialite; class AuthComponent extends Component { /** * @var array Associative array containing different types of authentication */ protected $typeOfAuth = [ 'google' => 'google', 'facebook' => 'facebook' ]; /** * Redirect to the corresponding social media authentication page * @return RedirectResponse */ public function handlerAuthRedirect() { $provider = $this->getProvider(); return Socialite::driver($provider)->redirect(); } /** * Handle social media authentication and redirect * * @param string $driver * @return void */ protected function handleSocialAuthRedirect($user) { if (app()->login(['email', $user->email])) { redirect('/home'); } } /** * Handle redirect after Google authentication. * @return void */ public function handlerRediretGoogleAuth() { $user = Socialite::driver('google')->user(); $this->handleSocialAuthRedirect($user); // Register a new user and redirect to home page $this->registerNewUserAndRedirect($user); } /** * Handle redirect after Facebook authentication. * @return void */ public function handlerRediretFacebookAuth() { $user = Socialite::driver('facebook')->user(); $this->handleSocialAuthRedirect($user); // Register a new user and redirect to home page $this->registerNewUserAndRedirect($user); } /** * Register new user and redirect * * @param object $user * @return void */ protected function registerNewUserAndRedirect(object $user) { $newUser = new User; $newUser->name = $user->name; $newUser->email = $user->email; $newUser->save(); app()->login($newUser); redirect('/home'); } /** * This function retrieves the authentication provider from the route parameters * @return string */ protected function getProvider(): string { $provider = app()->request->getRouteParam('provider'); if (!array_key_exists($provider, $this->typeOfAuth)) { throw new \Exception('Authentication provider unknown'); } return $this->typeOfAuth[$provider]; } }