Laravel 8 Socialite Github Login Tutorial Example
Laravel Socialite package makes it simple to authenticate users to Facebook, Twitter, Google, LinkedIn, GitHub, etc. You can authenticate your users for the purpose of connecting their pre-existing user account to a third-party service, but you can also use it as your primary login method, which we'll be talking about here.
In this tutorial, we will learn how to create a Github Login in Laravel socialite (Laravel 8 Socialite Github Login Tutorial).
so, we have seen many websites are using Github social login. but this tutorial we can easily create the Github social login using the laravel socialite package. so you can follow the below step.
Step 1: Install Laravel
Step 2: Setting Database Configuration
Step 3: Create Table using migration
Step 4: Install Package
Step 5: Add providers and aliases
Step 6: Create a Github App
Step 7: Configuration of API Key
Step 8: Create Auth
Step 9: Create Route
Step 10: Update Model and Controller
Step 11: Create Blade File
First of all, install laravel 8, so first open the terminal and go to xampp htdocs folder directory and using the command prompt. after then run the below command.
composer create-project --prefer-dist laravel/laravel laravel8_github_login
After the complete installation of laravel. we have to database configuration. now open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel8_github_login) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root)
We need to add github_id in the user table. We will add the github_id in the migration list after then we will run the migration command.
Here this file We have already updated in the database/migrations/create_users_table file. so you can see the below example code.
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->string('github_id')->nullable(); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } ?>
Run the below command. after the changes above file.
php artisan migrate
We are going to install the laravel Socialite package using the below command:
composer require laravel/socialite
We will add below providers and aliases in the “config/app.php” file
'providers' => [ .... Laravel\Socialite\SocialiteServiceProvider::class, ], 'aliases' => [ .... 'Socialite' => Laravel\Socialite\Facades\Socialite::class, ]
This step, we need to Github client id and client secret key and that credentials through we can create a successful login. if you don’t have twitter credentials then you have to create a Facebook app. so you can go on https://github.com/settings/developers and create it.
We are going to the configuration of the API key in the app/config/services.php file.
'github' => [ 'client_id' => 'enter your client id', 'client_secret' => 'enter your client secret key', 'redirect' => 'http://127.0.0.1:8000/callback/github', ],
This step, We will create laravel UI and authentication. below command.
composer require laravel/ui
php artisan ui bootstrap --auth
npm install
Add the following route code in the “routes/web.php” file.
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('githubLogin'); }); Route::get('auth/github', 'Auth\LoginController@redirectToGithub'); Route::get('auth/github/callback', 'Auth\LoginController@handleGithubCallback'); ?>
This step, we need to update the User.php model and LoginController.phpSo you can see the below example code.
app/User.php
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'github_id' ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; } ?>
app/Http/Controllers/Auth/LoginController.php
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Socialite; use Auth; use Exception; use App\User; 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 = '/home'; /*** Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } public function redirectToGithub() { return Socialite::driver('github')->redirect(); } public function handleGithubCallback() { try { $user = Socialite::driver('github')->user(); $finduser = User::where('github_id', $user->id)->first(); if ($finduser) { Auth::login($finduser); return return redirect('/home'); } else { $newUser = User::create(['name' => $user->name, 'email' => $user->email, 'github_id' => $user->id]); Auth::login($newUser); return redirect()->back(); } } catch(Exception $e) { return redirect('auth/github'); } } } ?>
Finally, We will create a githubLogin.blade.php file in the “resources/views/” folder directory and paste the below code.
githubLogin.blade.php
<div class="container"> <div class="row"> <div class="col-md-12 row-block"> <a class="btn btn-lg btn-primary btn-block" href="{{ url('auth/github') }}"> <strong>Login With Github</strong> </a> </div> </div> </div>
I hope it can help you...
Leave a Reply