Home > PHP Framework > Laravel > Laravel development: How to implement social login using Laravel Socialite?

Laravel development: How to implement social login using Laravel Socialite?

WBOY
Release: 2023-06-13 22:09:17
Original
1249 people have browsed it

In today's increasingly developing social network era, social login has become a standard feature of more and more websites. It not only facilitates users to log in quickly, but also helps the website increase user traffic and social interaction. Although it is not difficult to implement social login yourself, using ready-made social login components can save time and effort. In PHP development, Laravel Socialite is an easy-to-use social login component. Let's take a look at how to use it to implement social login.

1. Install Laravel Socialite

First we need to install the Socialite package in the Laravel application, use the following command:

composer require laravel/socialite

After the installation is complete, add the Socialite service provider in the config/app.php file:

'providers' => [
    // Other service providers...

    LaravelSocialiteSocialiteServiceProvider::class,
]
Copy after login

Then in the config/app.php file Add the Socialite facade alias:

'aliases' => [
    // Other aliases...

    'Socialite' => LaravelSocialiteFacadesSocialite::class,
]
Copy after login

In this way, we successfully installed the Socialite component.

2. Create a social login application

Socialite can support many different social login applications, and we need to create an application for each application. The following uses GitHub as an example to demonstrate creating a new OAuth application on GitHub.

  1. First, log in to your GitHub account, select Settings->Developer settings->OAuth Apps in the avatar drop-down menu to enter the OAuth Apps page.

  1. Click the New OAuth App button and fill in the following information:
  • Application name: application name;
  • Homepage URL: URL of the application homepage;
  • Authorization callback URL: The callback URL after successful social login authentication.

After completing the filling, click the Register application button.

  1. Generate application The client ID and client secret of the program.

3. Implement social login

  1. First, add a social login button to the application’s login page and link it to Socialite’s GitHub authentication service:
<a href="{{ url('/auth/github') }}">使用GitHub登录</a>
Copy after login
  1. Create GitHub login route in Laravel. Open the routes/web.php file and add the following route definition:
Route::get('auth/github', 'AuthLoginController@redirectToProvider');
Route::get('auth/github/callback', 'AuthLoginController@handleProviderCallback');
Copy after login

Here we are asked to redirect the user to the GitHub authentication service via redirectToProvider and Complete the login authentication there. When the authentication is successful, the GitHub authentication service will redirect to the specified callback URL auth/github/callback, and pass the successfully authenticated user information in the Session through this URL.

  1. Build the redirectToProvider function. Open the app/Http/Controllers/Auth/LoginController.php file and add the following implementation:
namespace AppHttpControllersAuth;

use IlluminateHttpRequest;
use AppHttpControllersController;
use Socialite;

class LoginController extends Controller
{
    // ...
    public function redirectToProvider()
    {
        return Socialite::driver('github')->redirect();
    }
    // ...
}
Copy after login

We call in Socialite driver() method to obtain an instance of the GitHub authentication driver, and use the redirect() method to redirect the user to the login authentication service.

  1. Next, we will handle the callback after successful authentication in the handleProviderCallback method. Open the app/Http/Controllers/Auth/LoginController.php file and add the following method:
// ...
public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('github')->user();
    } catch (Exception $e) {
        return redirect('/login');
    }

    // 验证用户信息并完成登录,代码省略
    // ...
}
// ...
Copy after login

We use user from Socialite () method obtains the user information returned by the GitHub authentication service. If there are any errors during the authentication process, we will redirect the user to the application's login page. If authentication is successful, the information in the $user object can be used to authenticate the user and complete the login operation.

4. Summary

At this point, we have completed the process of using Laravel Socialite to implement GitHub social login. Through simple configuration and some necessary code, we can easily implement multiple social login methods, adding some small but useful features to the application, which are recommended for use in actual projects.

The above is the detailed content of Laravel development: How to implement social login using Laravel Socialite?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template