humweb/sociable

Socialite认证和持久层实现。

安装次数: 17,583

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 3

分支: 0

开放问题: 0

类型:项目

2.1 2019-06-03 18:31 UTC

This package is auto-updated.

Last update: 2024-09-03 19:00:38 UTC


README

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

Socialite认证和持久层实现。

安装

通过Composer

$ composer require humweb/sociable

添加ServiceProvider

providers数组中添加此包的服务提供者。

Humweb\Sociable\ServiceProvider::class

在Laravel 5中发布配置

$ php artisan vendor:publish --provider="Humweb\Sociable\ServiceProvider"

运行数据库迁移

$ php artisan migrate"

配置用户认证驱动和提供者配置

<?php

// config/sociable.php

return [

    // Builtin options: laravel, sentinel
    'auth_provider' => 'laravel',

    // Optional provider list, mainly used to list login buttons.
    'providers' => [
        'google',
        'github',
        'twitter'
    ]
];

Sociable特质添加到用户模型

class User extends Authenticatable
{
    use Sociable;
}

入门

查看Humweb\Sociable\Http\Controllers\AuthController.php了解

  • OAuth登录
  • 将第三方服务链接到用户账户
示例AuthController(在正常登录后自动链接第三方账户)
<?php

class AuthController extends Controller
{
    /**
     * Login
     */
    public function getLogin(Request $request)
    {

        // Remove social data from session upon request
        if ($request->exists('forget_social')) {
            $request->session()->forget('social_link');
        }

        return view('login');
    }


    public function postLogin(Request $request)
    {

        // Gather credentials
        $credentials = [
            'username' => $request->get('username'),
            'password' => $request->get('password'),
        ];

        if ($user = \Auth::attempt($credentials, $remember)) {

            // Check for social data in session
            if ($request->session()->has('social_link')) {

                // Grab data from session
                $social = $request->session()->get('social_link');
                $user->attachProvider($social['provider'], $social);

                // Remove link data
                $request->session()->forget('social_link');

                return redirect()
                    ->intended('/')
                    ->with('success', 'Account connected to "'.$social['provider'].'" successfully.');
            }

            return redirect()->intended('/');
        }

        // Default error message
        return back()
            ->withInput()
            ->withErrors('Invalid login or password.');
    }

}
示例登录页面消息,用于删除会话信息

如果用户不想在登录后自动链接账户,这可能会很有用。

@if (session()->has('social_link'))
    <div class="alert alert-info">
        Login to link your "{{ session('social_link.provider') }}" and "<Your Site>" accounts. <br>
        If this is not what you want <a href="/login?forget_social">click here</a> to refresh.
    </div>
@endif

变更日志

请参阅CHANGELOG了解最近更改的信息。

测试

$ phpunit

安全

如果您发现任何安全相关的问题,请通过电子邮件::author_email联系,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件了解更多信息。