How to use the Hyperf framework for third-party login
Introduction:
With the development of the Internet, third-party login has become a standard for many websites and applications. Equipped with functions. Through third-party login, users can use their existing account information on the third-party platform to log in to other websites or applications, avoiding the cumbersome registration process and greatly improving the user experience. This article will introduce how to use the Hyperf framework to implement third-party login functionality, with specific code examples.
1. Preparation work
Before we start to implement third-party login, we need to prepare some necessary work:
2. Install dependency packages
Execute the following command in the project root directory to install the third-party login extension package provided by the Hyperf community.
composer require hyperf/socialite
3. Add configuration information
Add the socialite.php
configuration file in the config/autoload/
directory of the Hyperf project root directory, and add the following content :
<?php return [ 'default' => [ 'wechat' => [ 'client_id' => env('WECHAT_CLIENT_ID', ''), 'client_secret' => env('WECHAT_CLIENT_SECRET', ''), 'redirect' => env('WECHAT_REDIRECT', ''), ], 'qq' => [ 'client_id' => env('QQ_CLIENT_ID', ''), 'client_secret' => env('QQ_CLIENT_SECRET', ''), 'redirect' => env('QQ_REDIRECT', ''), ], // 更多第三方平台的配置信息... ], ];
WECHAT_CLIENT_ID
, WECHAT_CLIENT_SECRET
, WECHAT_REDIRECT
, QQ_CLIENT_ID
, QQ_CLIENT_SECRET# in the above configuration file Fields such as ##,
QQ_REDIRECT need to be replaced with the information applied by the developer on the corresponding platform.
Create a controller in the Hyperf project, such as
LoginController, and add the following method:
<?php declare(strict_types=1); namespace AppController; use HyperfHttpServerAnnotationController; use HyperfHttpServerAnnotationRequestMapping; use PsrHttpMessageResponseInterface; /** * @Controller */ class LoginController { /** * @RequestMapping(path="/login/{platform}", methods="GET") */ public function redirectToThirdParty(string $platform): ResponseInterface { return context() ->getContainer() ->get(HyperfSocialiteSocialiteManager::class) ->driver($platform) ->redirect(); } /** * @RequestMapping(path="/login/{platform}/callback", methods="GET") */ public function handleThirdPartyCallback(string $platform): ResponseInterface { $user = context() ->getContainer() ->get(HyperfSocialiteSocialiteManager::class) ->driver($platform) ->user(); // 在此处处理用户登录逻辑,例如创建用户、更新用户信息等 return $this->generateResponse(); } private function generateResponse(): ResponseInterface { // 生成登录成功后的响应 return $response; } }
Add a third-party login entrance to the page, for example:
<a href="/login/wechat">使用微信登录</a> <a href="/login/qq">使用QQ登录</a>
When the user completes login authorization on the third-party platform, it will jump back to our application and call the callback method
handleThirdPartyCallback. In this method, we can obtain the user's basic information on the third-party platform through the
user() method, such as avatar, nickname, etc. Here, we can determine whether the user has registered in our application based on the information returned by the third-party platform. If not, the user's automatic registration can be completed here.
After successful login, we can generate the user's login status and return a response to the user according to business needs, such as generating JWT Token, setting Cookie, etc.
By using the Hyperf framework and the
hyperf/socialite extension package provided by the community, we can quickly implement the third-party login function. In this article, we implement the function of user login through WeChat and QQ by introducing dependency packages, adding configuration information, creating controllers and other steps. Of course, in actual projects, we can also add more third-party login methods according to needs, such as Weibo, GitHub, etc. I hope this article can provide you with some reference for implementing third-party login function in Hyperf.
The above is the detailed content of How to use Hyperf framework for third-party login. For more information, please follow other related articles on the PHP Chinese website!