humweb / sociable by ryun

Socialite authentication and persistence layer implementation.
17,670
0
3
Package Data
Maintainer Username: ryun
Maintainer Contact: ryun@humboldtweb.com (Ryan Shonfner)
Package Create Date: 2016-08-05
Package Last Update: 2023-08-03
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-17 03:04:03
Package Statistics
Total Downloads: 17,670
Monthly Downloads: 20
Daily Downloads: 0
Total Stars: 0
Total Watchers: 3
Total Forks: 0
Total Open Issues: 0

Sociable

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

Socialite authentication and persistence layer implementation.

Install

Via Composer

$ composer require humweb/sociable

Add ServiceProvider

In the providers array add the service providers for this package.

Humweb\Sociable\ServiceProvider::class

Publish configuration in Laravel 5

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

Run database migration

$ php artisan migrate"

Configure user auth driver and provider configs

<?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'
    ]
];

Add Sociable trait to user model

class User extends Authenticatable
{
    use Sociable;
}

Getting started

See Humweb\Sociable\Http\Controllers\AuthController.php for:

  • OAuth login
  • Link third-party service to a user account
Example AuthController (Auto-link third-party account after normal login)
<?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.');
    }

}
Example message for your login page to remove session info

This can help if the user did not want to auto link the accounts after login.

@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

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ phpunit

Security

If you discover any security related issues, please email :author_email instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.