Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Laravel Socialite

From WikiOD

Installation[edit | edit source]

composer require laravel/socialite

This installation assumes you're using Composer for managing your dependencies with Laravel, which is a great way to deal with it.

Configuration[edit | edit source]

In your config\services.php you can add the following code

'facebook' => [
    'client_id' => 'your-facebook-app-id',
    'client_secret' => 'your-facebook-app-secret',
    'redirect' => 'http://your-callback-url',
],

You'll also need to add the Provider to your config\app.php

Look for 'providers' => [] array and, at the end of it, add the following

'providers' => [
      ...

      Laravel\Socialite\SocialiteServiceProvider::class,
]

A Facade is also provided with the package. If you would like to make usage of it make sure that the aliases array (also in your config\app.php) has the following code

'aliases' => [
    ....
    'Socialite' => Laravel\Socialite\Facades\Socialite::class,
]

Basic Usage - Facade[edit | edit source]

return Socialite::driver('facebook')->redirect();

This will redirect an incoming request to the appropriate URL to be authenticated. A basic example would be in a controller

<?php

namespace App\Http\Controllers\Auth;

use Socialite;

class AuthenticationController extends Controller {

    /**
     * Redirects the User to the Facebook page to get authorization.
     *
     * @return Response
     */
    public function facebook() {
        return Socialite::driver('facebook')->redirect();
    }

}

make sure your app\Http\routes.php file has a route to allow an incoming request here.

Route::get('facebook', 'App\Http\Controllers\Auth\AuthenticationController@facebook');

Basic Usage - Dependency Injection[edit | edit source]

/**
 * LoginController constructor.
 * @param Socialite $socialite
 */
public function __construct(Socialite $socialite) {
    $this->socialite = $socialite;
}

Within the constructor of your Controller, you're now able to inject the Socialite class that will help you handle login with social networks. This will replace the usage of the Facade.

/**
 * Redirects the User to the Facebook page to get authorization.
 *
 * @return Response
 */
public function facebook() {
    return $this->socialite->driver('facebook')->redirect();
}

Socialite for API - Stateless[edit | edit source]

public function facebook() {
    return $this->socialite->driver('facebook')->stateless()->redirect()->getTargetUrl();
}

This will return the URL that the consumer of the API must provide to the end user to get authorization from Facebook.

Credit:Stack_Overflow_Documentation