mad-web/laravel-social-auth

易于集成的社交认证,支持大量可用提供商

该包的规范仓库似乎已不存在,因此该包已被冻结。

4.0.0 2021-10-11 14:15 UTC

This package is auto-updated.

Last update: 2024-04-07 00:03:40 UTC


README

Stand With Ukraine

社交认证

Latest Version on Packagist Software License Build Status Code Style Quality Score Total Downloads

此包提供以下功能

  • 登录
  • 注册
  • 将社交网络提供商附加/分离到现有账户

此包基于 laravel/socialite,并提供了使用来自 Socialite Providers 的许多附加提供商的简便方法。

安装

通过Composer

$ composer require mad-web/laravel-social-auth

并在config/app.php文件中添加服务提供者

'providers' => [
    // ...
    MadWeb\SocialAuth\SocialAuthServiceProvider::class,
];

接下来,使用以下命令发布迁移

$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="migrations"

该包假设您的用户表名为"users"。如果这不是情况,您应手动编辑已发布的迁移以使用您自定义的表名。

迁移发布后,您可以通过运行迁移来创建存储支持的提供者的social_providers表以及用于将提供者附加到用户的user_has_social_provider交叉表

$ php artisan migrate

您可以使用以下命令发布配置文件

$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="config"

这是已发布config/social-auth.php配置文件的 内容

return [

    /*
    |--------------------------------------------------------------------------
    | Additional service providers
    |--------------------------------------------------------------------------
    |
    | The social providers listed here will enable support for additional social
    | providers which provided by https://socialiteproviders.github.io/ just
    | add new event listener from the installation guide
    |
    */
    'providers' => [
        //
    ],

    'models' => [
        /*
         * When using the "UserSocialite" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your available social providers. Of course, it
         * is often just the "SocialProvider" model but you may use whatever you like.
         */
        'social' => \MadWeb\SocialAuth\Models\SocialProvider::class,

        /*
         * User model which you will use as "SocialAuthenticatable"
         */
        'user' => \App\User::class,
    ],

    'table_names' => [

       /*
       |--------------------------------------------------------------------------
       | Users Table
       |--------------------------------------------------------------------------
       |
       | The table for storing relation between users and social providers. Also there is
       | a place for saving "user social network id", "token", "expiresIn" if it exist
       |
       */
        'user_has_social_provider' => 'user_has_social_provider',

        /*
        |--------------------------------------------------------------------------
        | Social Providers Table
        |--------------------------------------------------------------------------
        |
        | The table that contains all social network providers which your application use.
        |
        */
        'social_providers' => 'social_providers'
    ],

    'foreign_keys' => [

        /*
         * The name of the foreign key to the users table.
         */
        'users' => 'user_id',

        /*
         * The name of the foreign key to the socials table
         */
        'socials' => 'social_id'
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication redirection
    |--------------------------------------------------------------------------
    |
    | Redirect path after success/error login via social network
    |
    */
    'redirect' => '/home'
];

或者,您可以使用以下命令发布和修改视图模板

$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="views"

此外,您还可以发布和修改翻译文件

$ php artisan vendor:publish --provider="MadWeb\SocialAuth\SocialAuthServiceProvider" --tag="lang"
将凭据添加到您的项目中

将提供者添加到config/services.php

'facebook' => [
    'client_id' => env('FB_ID'),
    'client_secret' => env('FB_SECRET'),
    'redirect' => env('FB_REDIRECT'),
],

'google' => [
    'client_id' => env('GOOGLE_ID'),
    'client_secret' => env('GOOGLE_SECRET'),
    'redirect' => env('GOOGLE_REDIRECT'),
],

'github' => [
    'client_id' => env('GITHUB_ID'),
    'client_secret' => env('GITHUB_SECRET'),
    'redirect' => env('GITHUB_REDIRECT'),
]

将凭据添加到.env

FB_ID=
FB_SECRET=
FB_REDIRECT=https://app.domain/social/facebook/callback

GOOGLE_ID=
GOOGLE_SECRET=
GOOGLE_REDIRECT=https://app.domain/social/google/callback

GITHUB_ID=
GITHUB_SECRET=
GITHUB_REDIRECT=https://app.domain/social/github/callback

然后,在数据库中创建您的社交提供者。

使用控制台命令

php artisan social-auth:add google --label=Google+

通过模型,例如在seeder中

SocialProvider::create(['slug' => 'google', 'label' => 'Google+']);

或直接添加记录。

您可以在社交认证请求中添加额外的范围和参数

SocialProvider::create([
    'label' => 'github',
    'slug' => 'Github',
    'scopes' => ['foo', 'bar'],
    'parameters' => ['foo' => 'bar']
]);

以覆盖默认范围

$SocialProvider->setScopes(['foo', 'bar'], true);
将社交按钮包含到您的模板中
 @include('social-auth::attach') // for authenticated user to attach/detach another socials
 @include('social-auth::buttons') // for guests to login via
准备您的用户模型

实现SocialAuthenticatable接口并将UserSocialite特质添加到您的User模型中

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MadWeb\SocialAuth\Traits\UserSocialite;
use MadWeb\SocialAuth\Contracts\SocialAuthenticatable;

class User extends Model implements SocialAuthenticatable
{
    use UserSocialite;
   ...
}

附加提供商

要使用来自 socialiteproviders.netlify.com 的任何附加提供商,首先安装它

composer require socialiteproviders/instagram

然后,将指南中的事件监听器添加到social-auth配置文件中

/*
|--------------------------------------------------------------------------
| Additional service providers
|--------------------------------------------------------------------------
|
| The social providers listed here will enable support for additional social
| providers which provided by https://socialiteproviders.netlify.com just
| add new event listener from the installation guide
|
*/
'providers' => [
    SocialiteProviders\Instagram\InstagramExtendSocialite::class,
],
...

自定义

路由

如果您需要对社交流程进行一些自定义,您应定义自己的控制器并将自定义URL放入路由文件中。

例如

Route::get('social/{social}', 'Auth\SocialAuthController@getAccount');
Route::get('social/{social}/callback', 'Auth\SocialAuthController@callback');
Route::get('social/{social}/detach', 'Auth\SocialAuthController@detachAccount');

如果不需要任何特殊功能,您可以使用我们默认的控制器。

自定义用户模型

我们从social-auth.models.user中获取用户模型。

用户属性映射

SocialAuthenticatable接口包含用于将社交字段映射到用户模型的方法mapSocialData。如果您需要自定义数据映射,您可以在User模型中覆盖此方法以满足项目偏好。

默认映射方法

public function mapSocialData(User $socialUser)
{
    $raw = $socialUser->getRaw();
    $name = $socialUser->getName() ?? $socialUser->getNickname();
    $name = $name ?? $socialUser->getEmail();

    $result = [
        $this->getEmailField() => $socialUser->getEmail(),
        'name' => $name,
        'verified' => $raw['verified'] ?? true,
        'avatar' => $socialUser->getAvatar(),
    ];

    return $result;
}

更改日志

有关最近更改的更多信息,请参阅CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅CONTRIBUTINGCONDUCT

安全性

如果您发现任何与安全相关的问题,请通过电子邮件 madweb.dev@gmail.com 通知,而不是使用问题跟踪器。

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件