Laravel Socialite

Source: https://github.com/laravel/socialite

I want to practice ‘socialite’ app given by Laravel 5.3 documentation. Installation:

  1. Open hosts file and Add a new domain ‘socialite.app’ at the file

    Add this line in the file:
  2. Edit ‘Homestead.yaml’ file in ~/.homestead/ to add the new domain ‘socialite.app’ and set the mapping
  3. Start vagrant with ‘vagrant up’ or if it already started up, refresh it with ‘vagrant provision’ then go into the ssh with ‘vagrant ssh’
  4. Go into ‘Code’ directory then create a new laravel project ‘socialite’
  5. Test it at our browser with http://socialite.app/. If the laravel screen is showed up, we are on the right path.
  6. Go into the new project ‘socialite’ root directory then add a dependency ‘laravel/socialite’

     
  7. Configuration
    After installing the Socialite library, register the Laravel\Socialite\SocialiteServiceProvider in your config/app.php configuration file:

    Also, add the Socialite facade to the aliases array in your app configuration file:

     
  8. You will also need to add credentials for the OAuth services your application utilizes. These credentials should be placed in your config/services.php configuration file, and should use the key facebook, twitter, linkedin, google, github or bitbucket, depending on the providers your application requires. For example for facebook credentials:

    we will use env file (.env) to store facebook credentials.

    SORRY, ONLY ADMIN CAN SHOW THIS!
    REMEMBER ‘http://socialite.app/auth/facebook/callback’ IS CALLBACK URL (AFTER THE SUCCESSFUL LOGIN ON FACEBOOK). ALSO IT MUST MATCH WITH ROUTE ‘auth/facebook/callback’ IN /routes/web.php LATER!
  9. Basic Usage
    Next, you are ready to authenticate users! You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication. We will access Socialite using the Socialite facade. Please create a new controller file ‘AuthController.php’ in /app/Http/Controllers/Auth/ to work with facebook login:

    NOTE: DONT FORGET TO ADD ‘use App\Http\Controllers\Controller;’ BEFORE ‘use Socialite;’ BECAUSE I GOT AN ERROR ABOUT IT!
  10. Creating Views. Create two new blade files. They are /resources/views/home.blade.php and /resources/views/master.blade.php:
    master.blade.php:

    home.blade.php:

    url(‘/auth/facebook’) would redirect the page to facebookpage!
  11. Set the routes. Edit /routes/web.php like this:

    We set the root (‘/’) url would open the home page (home.blade.php)
  12. Test it! http://socialite.app/
    Here is the screensocialite-homeIf we click ‘Facebook’ link, it would redirect us to facebook page. The url would be like this: https://www.facebook.com/v2.8/dialog/oauth?client_id=166568033366882&redirect_uri=http%3A%2F%2Fsocialite.app&scope=email&response_type=code&state=es6ub834Woh7xDxTvdqru84U7kwiRJkftD83HFz7
    If I get this pagesocialite-fb-page Then I need to modify my facebook app to add this domain (http://socialite.app/) at ‘Site URL’ box. To do it, Open your app from https://developers.facebook.com/apps, then Click your app. Then at Settings->Basic->at Site Url fill with this domain name: http://socialite.app/. Then Save and fix any error.
    SORRY, ONLY ADMIN CAN SHOW THIS!
    Here is the oauth2 output from facebook for successful login:
    SORRY, ONLY ADMIN CAN SHOW THIS!
    Here is the callback result the successful facebook loginsocialite-fb-callback
  13. How to add the others social links (twitter, github, google plus and linkedin)?
    Please read Complete Laravel 5 Socialite tutorial to find out how to do it.
    You need to setup your apps for the above social app to get the credentials like Client ID and Client Secret and the redirect URL.
    To setup Google+ app, please read Google APIs
    To setup twitter app, please read Twitter Socialite App
    To setup github app, please read Github App
    To setup linkedin app, please read Linkedin Oauth2 App
    Then modify .env file to include above credentials:
    SORRY, ONLY ADMIN CAN SHOW THIS!
  14. Here I want to make the redirect URL to be uniform like this:

    So modify our social routes in /routes/web.app like this:

     
  15. Modify /config/services.php to get all the credentials from .env file

     
  16. Modify /resources/views/home.blade.php to include all the social app
  17. Modify /app/Http/Controllers/Auth/AuthController.php to add a parameter $provider
  18. Run on your browsersocialite-home-mutiLogin via Facebook RUN WELL LIKE BEFORE!
    Login via Twitter, For the first time we are needed to authorize the applicationsocialite-twitter-autorizeClick ‘Authorize app’. If nothing wrong, here is the callback resultsocialite-twitter-callbackSORRY, ONLY ADMIN CAN SHOW THIS!
    Login via Google, For the first time we are needed to authorize the applicationsocialite-google-autorizeClick ‘Izinkan’. If nothing wrong, here is the callback resultsocialite-google-callback SORRY, ONLY ADMIN CAN SHOW THIS!
    Login via Github, For the first time we are needed to authorize the applicationsocialite-github-autorizeClick ‘Authorize application’. If nothing wrong, here is the callback resultsocialite-github-callbackSORRY, ONLY ADMIN CAN SHOW THIS!
    Login via Linkedin, For the first time we are needed to authorize the applicationsocialite-linkedin-autorizeClick ‘Allow’. If nothing wrong, here is the callback resultsocialite-linkedin-callbackSORRY, ONLY ADMIN CAN SHOW THIS!
  19. OKAY! EVERYTHING RUN WELL. THE NEXT STEP IS TO USE DATABASE/SESSION TO SAVE THE LOGIN INFORMATION!
    For this, please read : Laravel 5.2 Socialite Facebook Login and Using Github authentication for login with Laravel Socialite.

Leave a Reply

Your email address will not be published. Required fields are marked *