Home  >  Article  >  PHP Framework  >  How to use Laravel to implement third-party login function

How to use Laravel to implement third-party login function

王林
王林Original
2023-11-02 11:01:111282browse

How to use Laravel to implement third-party login function

In recent years, with the continuous development of Internet technology, people have higher and higher requirements for the security and user experience of websites or applications. In order to facilitate user registration and login, third-party account login has become one of the commonly used functions in websites and applications. As a popular PHP framework, Laravel provides a convenient social login authorization function, making it particularly convenient to implement third-party login functions. This article will introduce how to use Laravel to implement the third-party login function, and provide some implementation code for readers' reference.

1. Configure service provider information

In Laravel, we can use the social login authorization package socialite to implement the third-party login function. socialite supports a variety of authorized service providers, including Facebook, Google, Twitter, WeChat, etc. Therefore, before using socialite to implement the third-party login function, we need to configure the service provider information.

Taking GitHub as an example, by creating and authorizing an OAuth application, we can obtain the client ID and client key. The specific acquisition method is as follows:

  1. Visit the GitHub website and log in to the account.
  2. Enter GitHub's settings page, select the "Developer settings" tab, click "OAuth Apps", and then select "New OAuth App".
  3. Fill in the details of the application, including application name, application home page URL, authorization callback URL, etc.
  4. Submit and create the application. After successful creation, information such as the client ID and client key of the application can be found on the GitHub page.

With this information in hand, we can create a Laravel configuration file named /config/services.php and add the following content to it:

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => 'http://localhost:8000/login/github/callback',
],

Among them, github is the name of the service provider we want to use, client_id and client_secret are the customers we obtained when applying for the OAuth application The client ID and client secret, redirect is the OAuth callback URL, it is the authorization callback URL we filled in when creating the application. Here we set it to localhost:8000/login/github/callback. We will create this route in the routing file later.

2. Create a route

Next, we need to create a route in the routing file of the Laravel application to implement the function of logging in through GitHub. Here, we can use Laravel’s Route class to create routes.

The first step is to create an authorization redirect route, which is responsible for redirecting users to the GitHub login page. Add the following code in the routing file:

Route::get('login/github', function () {
    return Socialite::driver('github')->redirect();
});

Among them, Socialite::driver('github') calls the driver of the socialite authorization package Method to obtain the instance of the GitHub service provider. The redirect method implements the OAuth authorization jump and redirects the user to GitHub's authentication and authorization page.

In the second step, we create an authorization callback route, which is responsible for obtaining the user information returned by the GitHub service provider and processing it. Add the following code in the routing file:

Route::get('login/github/callback', function () {
    $user = Socialite::driver('github')->user();
    // 处理用户信息
});

Among them, Socialite::driver('github') calls the driver of the socialite authorization package Method to obtain the instance of the GitHub service provider. The user method implements the process of obtaining user information through OAuth authorization. It will obtain the user's information from the GitHub service provider, and the information will be returned in the form of the $user variable.

After obtaining the $user variable, we can process the user information. Since the format of user information returned by each third-party service provider is slightly different, we will not go into details here.

3. Create public methods

In order to facilitate management and use, we can create in the app/Http/Controllers/Auth/LoginController.php controller GitHub method, for other methods to call.

In this method, we first need to use the Socialite::driver('github')->redirect() method to redirect the user to the GitHub login page. During the callback process of obtaining user information, we can process the user information in the GitHubCallback method.

The following is a basic sample code:

<?php
namespace AppHttpControllersAuth;

use AppHttpControllersController;
use Socialite;

class LoginController extends Controller
{
    /**
     * 重定向到GitHub登录页面
     *
     * @return Response
     */
    public function GitHub()
    {
        return Socialite::driver('github')->redirect();
    }

    /**
     * 从GitHub获取用户信息
     *
     * @return Response
     */
    public function GitHubCallback()
    {
        $user = Socialite::driver('github')->user();
        dd($user);
    }
}

4. Using the expansion package

If you feel that the above method is cumbersome, you can use the social login authorization expansion packagelaravel/socialite, this extension package has encapsulated major service providers and provided corresponding command line tools to construct authorized service providers.

  1. Installationlaravel/socialite

Use composer in the terminal to install laravel/socialiteExtension package:

composer require laravel/socialite
  1. Configuring service provider information

In Laravel’s configuration file config/services.php, add the service provider information you need to use. Take GitHub as an example below. :

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => '/auth/github/callback',
],
  1. Create authorization route

In the routes/web.phprouting file, create an authorization route that is not verified:

Route::get('/auth/github', 'AuthLoginController@GitHub');
  1. 创建回调路由

routes/web.php路由文件中,创建一个授权回调路由:

Route::get('/auth/github/callback', 'AuthLoginController@GitHubCallback');
  1. 创建控制器

app/Http/Controllers/Auth目录下,创建一个名为LoginController.php的控制器。控制器中添加方法GitHubGitHubCallback实现授权和回调过程,具体代码如下:

use Socialite;

class LoginController extends Controller
{

    public function GitHub()
    {
        return Socialite::driver('github')->redirect();
    }

    public function GitHubCallback()
    {
        $user = Socialite::driver('github')->user();
        dd($user);
    }

}

至此,我们已经成功使用laravel/socialite扩展包实现了第三方登录的功能。

总结:

本文介绍了如何使用Laravel框架的社交化登录授权包socialite,在实现第三方登录功能方面变得尤为便利。通过配置服务商信息、创建路由以及使用扩展包等方法,我们可以轻松地实现通过GitHub登录的功能。希望这篇文章能够对各位开发人员有所帮助。

The above is the detailed content of How to use Laravel to implement third-party login function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn