berejant / kneu-laravel-socialite-provider by berejant

KNEU Provider for laravel/socialite package.
Package Data
Maintainer Username: berejant
Maintainer Contact: (Anton Bereezhnyi)
Package Create Date: 2016-11-28
Package Last Update: 2021-01-22
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2022-11-23 15:29:48
Package Statistics
Total Downloads: 85
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 1
Total Open Issues: 0

KNEU Laravel Socialite

KNEU Provider for Laravel Socialite Providers.


1. Composer

Add package to your laravel project via composer

composer require kneu/laravel-socialite-provider

2. Add service provider

  • Remove Laravel\Socialite\SocialiteServiceProvider from your providers[] array in config\app.php if you have added it already.
  • Add \SocialiteProviders\Manager\ServiceProvider::class to your providers[] array in config\app.php.

For example:

    'providers' => [
        // remove 'Laravel\Socialite\SocialiteServiceProvider',
        \SocialiteProviders\Manager\ServiceProvider::class, // add

3. Add the Event and Listeners

  • Add SocialiteProviders\Manager\SocialiteWasCalled event to your listen[] array in <app_name>/Providers/EventServiceProvider.

  • Add listener '\SocialiteProviders\Kneu\KneuExtendSocialite@handle' to the SocialiteProviders\Manager\SocialiteWasCalled[].

For example:

    protected $listen = [
        \SocialiteProviders\Manager\SocialiteWasCalled::class  => [

4. Environment Variables

Append provider values to your .env file


Basic Usage

Next, you are ready to authenticate users via KNEU! You will need tree routes:

  • first for redirecting the user to the KNEU OAuth provider
  • second for receiving the callback from the KNEU provider after authentication
  • third for logout.

Example of Controller


namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\User;
use Laravel\Socialite\Facades\Socialite;
use SocialiteProviders\Manager\OAuth2\User as KneuUser;

class LoginController extends Controller
     * Where to redirect users after login.
     * @var string
    protected $redirectTo = '/';

    public function __construct()
        $this->middleware('guest', ['except' => 'logout']);

     * @return \SocialiteProviders\Kneu\Provider
    protected function getProvider()
        return Socialite::with('kneu');

    public function login(Request $request)
        $request->session()->put('url.intended', url()->previous());

        return $this->getProvider()->redirect();

    public function loginComplete()
        /** @var KneuUser $kneuUser */
        $kneuUser = $this->getProvider()->user();

        $user = User::withTrashed()->find($kneuUser->id);
        if(!$user) {
            $user = new User();

        $user->trashed() ? $user->restore() : $user->touch();


        return redirect()->intended($this->redirectTo);

    public function logout(Request $request)



        return $this->getProvider()->logoutRedirect(url()->previous());


Of course, you will need to define routes to your controller methods:

Route::get('/login', 'Auth\LoginController@login');
Route::get('/login/complete', 'Auth\LoginController@loginComplete');
Route::get('/logout', 'Auth\LoginController@logout');

Retrieving User Details

Once you have a user instance, you can grab a few more details about the user:

$user = Socialite::driver('github')->user();

// OAuth Two Providers
$token = $user->token;
$refreshToken = $user->refreshToken; // currently not provided by
$expiresIn = $user->expiresIn;

$user->type; // enum ['student', 'teacher', 'simple']
$user->sex; // only for student, but not always correct

More details

See documentation for Laravel Socialite Providers.