<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Akinwunmi Damilola</title>
    <description>The latest articles on DEV Community by Akinwunmi Damilola (@damoscki11).</description>
    <link>https://dev.to/damoscki11</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F468528%2F4f98dd1d-67de-437b-a552-2022633c48f0.jpeg</url>
      <title>DEV Community: Akinwunmi Damilola</title>
      <link>https://dev.to/damoscki11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damoscki11"/>
    <language>en</language>
    <item>
      <title>Laravel Socialite - Sign In with GitHub (Laravel 8.0)</title>
      <dc:creator>Akinwunmi Damilola</dc:creator>
      <pubDate>Sun, 13 Sep 2020 20:34:06 +0000</pubDate>
      <link>https://dev.to/damoscki11/laravel-socialite-sign-in-with-github-laravel-8-0-467e</link>
      <guid>https://dev.to/damoscki11/laravel-socialite-sign-in-with-github-laravel-8-0-467e</guid>
      <description>&lt;h3&gt;
  
  
  Laravel
&lt;/h3&gt;

&lt;p&gt;Laravel is a free , open-source PHP software framework, developed by Taylor Otwell and designed to build web applications based on the architectural template model – view – controller (MVC) and based on Symfony. Some of Laravel 's features include a modular packaging framework with a dedicated dependency manager, various ways of accessing relational databases, utilities that help in application deployment and maintenance.&lt;br&gt;
Read more about &lt;a href="https://laravel.com" rel="noopener noreferrer"&gt;Laravel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A social login is a form of single sign-on using existing information from a social networking service such as Facebook, Twitter, Github or Google, to sign into a third-party application instead of creating a new account specifically for that application.&lt;br&gt;
With Facebook , Twitter , Google, LinkedIn, GitHub, GitLab and Bitbucket, Laravel Socialite offers an intuitive, fluent interface to OAuth authentication. It handles almost all of the social authentication code you hate writing on boilerplate and makes it easier for the user to sign up or sign in into an application with just a click.&lt;/p&gt;

&lt;p&gt;In this article, I will show you how to create a social sign up with GitHub using the Laravel framework.&lt;/p&gt;

&lt;h4&gt;
  
  
  Requirements
&lt;/h4&gt;

&lt;p&gt;Before you continue with this tutorial, it is assumed that you are familiar with PHP and Laravel Framework. If you are not really familiar or you haven't used the Laravel framework before, I will advise you to learn the basics of Laravel and can at least develop an application with the Laravel framework.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's get started!
&lt;/h4&gt;

&lt;h2&gt;
  
  
  Step 1
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Create Laravel Project
&lt;/h4&gt;

&lt;p&gt;First, let's create our Laravel project.&lt;/p&gt;

&lt;p&gt;Open your terminal, navigate to the folder you want to create your project and run the following command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer create-project --prefer-dist laravel/laravel socialite


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;cd socialite&lt;/code&gt; to navigate into the new project you just created&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Setup
&lt;/h4&gt;

&lt;p&gt;Open the project with any &lt;code&gt;code editor&lt;/code&gt; of your choice. I will be using Visual Studio Code.&lt;/p&gt;

&lt;p&gt;Configure your &lt;code&gt;.env file&lt;/code&gt; and connect to your database&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel-socialite
DB_USERNAME=root
DB_PASSWORD=


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open your terminal and run the following&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require laravel/ui


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan ui vue --auth


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install &amp;amp;&amp;amp; npm run dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then let's check out what we have so far. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

php artisan serve


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you successfully run the above, you should have something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599944767906%2FmKUMfEnV0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599944767906%2FmKUMfEnV0.png" alt="Laravel - Google Chrome 9_12_2020 10_03_07 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go back to you code editor, open the login.blade.php file and replace it with the following&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@extends('layouts.app')

@section('content')
&amp;lt;div class="container"&amp;gt;
    &amp;lt;div class="row justify-content-center"&amp;gt;
        &amp;lt;div class="col-md-8"&amp;gt;
            &amp;lt;div class="card"&amp;gt;
                &amp;lt;div class="card-header"&amp;gt;{{ __('Login') }}&amp;lt;/div&amp;gt;

                &amp;lt;div class="card-body"&amp;gt;
                    &amp;lt;form method="POST" action="{{ route('login') }}"&amp;gt;
                        @csrf

                        &amp;lt;div class="form-group row"&amp;gt;
                            &amp;lt;label for="email" class="col-md-4 col-form-label text-md-right"&amp;gt;{{ __('E-Mail Address') }}&amp;lt;/label&amp;gt;

                            &amp;lt;div class="col-md-6"&amp;gt;
                                &amp;lt;input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus&amp;gt;

                                @error('email')
                                    &amp;lt;span class="invalid-feedback" role="alert"&amp;gt;
                                        &amp;lt;strong&amp;gt;{{ $message }}&amp;lt;/strong&amp;gt;
                                    &amp;lt;/span&amp;gt;
                                @enderror
                            &amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;

                        &amp;lt;div class="form-group row"&amp;gt;
                            &amp;lt;label for="password" class="col-md-4 col-form-label text-md-right"&amp;gt;{{ __('Password') }}&amp;lt;/label&amp;gt;

                            &amp;lt;div class="col-md-6"&amp;gt;
                                &amp;lt;input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password"&amp;gt;

                                @error('password')
                                    &amp;lt;span class="invalid-feedback" role="alert"&amp;gt;
                                        &amp;lt;strong&amp;gt;{{ $message }}&amp;lt;/strong&amp;gt;
                                    &amp;lt;/span&amp;gt;
                                @enderror
                            &amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;

                        &amp;lt;div class="form-group row"&amp;gt;
                            &amp;lt;div class="col-md-6 offset-md-4"&amp;gt;
                                &amp;lt;div class="form-check"&amp;gt;
                                    &amp;lt;input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}&amp;gt;

                                    &amp;lt;label class="form-check-label" for="remember"&amp;gt;
                                        {{ __('Remember Me') }}
                                    &amp;lt;/label&amp;gt;
                                &amp;lt;/div&amp;gt;
                            &amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;

                        &amp;lt;div class="form-group row mb-0"&amp;gt;
                            &amp;lt;div class="col-md-8 offset-md-4"&amp;gt;
                                &amp;lt;button type="submit" class="btn btn-primary"&amp;gt;
                                    {{ __('Login') }}
                                &amp;lt;/button&amp;gt;

                                @if (Route::has('password.request'))
                                    &amp;lt;a class="btn btn-link" href="{{ route('password.request') }}"&amp;gt;
                                        {{ __('Forgot Your Password?') }}
                                    &amp;lt;/a&amp;gt;
                                @endif
                            &amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;

                        &amp;lt;div class="form-group row mb-0 mt-3"&amp;gt;
                            &amp;lt;div class="col-md-8 offset-md-4"&amp;gt;
                                &amp;lt;a href="{{ url('/login/github') }}" class="btn btn-warning"&amp;gt;
                                    {{ __('Login with Github') }}
                                &amp;lt;/a&amp;gt;
                            &amp;lt;/div&amp;gt;
                        &amp;lt;/div&amp;gt;


                    &amp;lt;/form&amp;gt;
                &amp;lt;/div&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
@endsection




&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After replacing the &lt;code&gt;login.blade.php&lt;/code&gt; you will have something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599949996567%2FSEWSn3Od7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599949996567%2FSEWSn3Od7.png" alt="Laravel - Google Chrome 9_12_2020 10_59_27 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Socialite Installation and Configuration
&lt;/h4&gt;

&lt;p&gt;Let us install Laravel socialite&lt;br&gt;
Run the following command in the root directory of your Laravel project to add the package to your project’s dependencies:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require laravel/socialite


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We need to register the service provider in &lt;code&gt;config / app.php&lt;/code&gt; once the installation is complete.&lt;/p&gt;

&lt;p&gt;In the providers array, of &lt;code&gt;config/app.php&lt;/code&gt; add&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Laravel\Socialite\SocialiteServiceProvider::class,


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the aliases array of the same file, add&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

'Socialite' =&amp;gt; Laravel\Socialite\Facades\Socialite::class,


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In your Laravel software, Socialite is all set up and you are ready to introduce social login.&lt;/p&gt;

&lt;p&gt;Add the following to &lt;code&gt;config/services.php&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

'github' =&amp;gt; [
        'client_id' =&amp;gt; env('GITHUB_ID'),
        'client_secret' =&amp;gt; env('GITHUB_SECRET'),
        'redirect' =&amp;gt; env('GITHUB_URL'),
    ],



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the following to &lt;code&gt;.env&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


GITHUB_ID=
GITHUB_SECRET=
GITHUB_URL=http://127.0.0.1:8000/github/callback



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s go to &lt;a href="https://github.com/settings/developers" rel="noopener noreferrer"&gt;https://github.com/settings/developers&lt;/a&gt; and create a new app. We have put app name, description, website name, and callback URL on this page :&lt;/p&gt;

&lt;p&gt;You will see your dashboard. Now get the following CLIENT ID and CLIENT SECRET.&lt;br&gt;
Copy the APP ID and App Secret content from the Dashboard settings into GITHUB ID and GITHUB SECRET, respectively.&lt;/p&gt;

&lt;p&gt;Go to &lt;code&gt;app/User.php&lt;/code&gt; and add &lt;code&gt;provider&lt;/code&gt; and &lt;code&gt;provider_id&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

'name', 'email', 'password', 'provider', 'provider_id',


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to your database/migration folder, replace the user schema with the following&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table-&amp;gt;id();
            $table-&amp;gt;string('name');
            $table-&amp;gt;string('email')-&amp;gt;nullable();
            $table-&amp;gt;timestamp('email_verified_at')-&amp;gt;nullable();
            $table-&amp;gt;string('password')-&amp;gt;nullable();
            $table-&amp;gt;string('provider');
            $table-&amp;gt;string('provider_id');
            $table-&amp;gt;rememberToken();
            $table-&amp;gt;timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;php artisan migrate&lt;/code&gt; command&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Route and Controller
&lt;/h4&gt;

&lt;p&gt;We will create two routes in the &lt;code&gt;web.php&lt;/code&gt; file. Go to app/routes/web.php file and create two below routes here &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Route::get('login/{provider}', 'App\Http\Controllers\Auth\LoginController@redirectToProvider');
Route::get('{provider}/callback', 'App\Http\Controllers\Auth\LoginController@handleProviderCallback');


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, replace the &lt;code&gt;LoginController.php&lt;/code&gt; with the following &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Socialite;
use App\Models\User;
use Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this-&amp;gt;middleware('guest')-&amp;gt;except('logout');
    }

    /**
     * Redirect the user to the provider authentication page.
     *
     * @return Response
     */
    public function redirectToProvider($provider)
    {
        return Socialite::driver($provider)-&amp;gt;redirect();
    }


    /**
     * Obtain the user information from provider.
     *
     * @return Response
     */
    public function handleProviderCallback($provider)
    {
        $user = Socialite::driver($provider)-&amp;gt;user();
        $authUser = $this-&amp;gt;findOrCreateUser($user, $provider);
        Auth::login($authUser, true);
        return redirect($this-&amp;gt;redirectTo);
    }

    /**
     * If a user has registered before using social auth, return the user
     * else, create a new user object.
     * @param  $user Socialite user object
     * @param $provider Social auth provider
     * @return  User
     */
    public function findOrCreateUser($user, $provider)
    {
        $authUser = User::where('provider_id', $user-&amp;gt;id)-&amp;gt;first();
        if ($authUser) {
            return $authUser;
        }
        return User::create([
            'name'     =&amp;gt; $user-&amp;gt;name,
            'email'    =&amp;gt; $user-&amp;gt;email,
            'provider' =&amp;gt; $provider,
            'provider_id' =&amp;gt; $user-&amp;gt;id
        ]);
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Go to your browser and click on &lt;code&gt;login with github&lt;/code&gt;&lt;br&gt;
You will redirected to your github account. After a successfully login your DB should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599950275112%2F0Glgs6iG5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599950275112%2F0Glgs6iG5.png" alt="Laravel - Google Chrome 9_12_2020 10_57_30 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599950214209%2Fi17tcpBaD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1599950214209%2Fi17tcpBaD.png" alt="Laravel - Google Chrome 9_12_2020 10_57_41 PM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this tutorial, We have successfully login with GitHub Credentials using &lt;a href="https://laravel.com/docs/7.x/socialite#introduction" rel="noopener noreferrer"&gt;Socialite&lt;/a&gt;  — The official package provided by Laravel for OAuth authentication with the Laravel 8.0.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
