How to login with GitHub account in Laravel 6 Application?

How to login with GitHub account in Laravel 6 Application?

In this Laravel tutorial, you will learn how to login with GitHub account in Laravel 6 application. We will use the Laravel socialite package

In this example,I will show you how to login with github account in Laravel 6 Application. We will use Laravel socialite package for login with Github account in Laravel 6 Application.

Here we will learn how to add a github social login in your Laravel 6 Application and how to simple authenticate users using the Github Login.

Follow below step to use social authentication in your application with github:

Install Laravel 6

you will create laravel 6 fresh application.

composer create-project --prefer-dist laravel/laravel loginWithGithub
Install Socialite

In this step we will install Socialite Package that provide fb api to connect with github account. So, first open your terminal and run bellow command.

composer require laravel/socialite
config/app.php

After install above package we should add providers and aliases in config file.

'providers' => [

    ....

    Laravel\Socialite\SocialiteServiceProvider::class,


],

'aliases' => [

    ....

    'Socialite' => Laravel\Socialite\Facades\Socialite::class,

],
Install Auth

First you need to install laravel/ui package as like bellow:

composer require laravel/ui

You can use following commands for creating auth:

php artisan ui bootstrap --auth

Now you need to run npm command, otherwise you can not see better layout of login and register page.

Install NPM:
npm install
Run NPM:
npm run dev
Create Github App

In this step we need github client id and secret that way we can get information of other user. so if you don't have github app account then you can create from here :

Now you have to click on application name and you will get client id and client secret like as bellow screen :

config/services.php

you will create the index.php file in the views directory.

return [

    ....

    'github' => [

        'client_id' => 'xxx',

        'client_secret' => 'xxx',

        'redirect' => 'http://localhost:8000/callback/github',

    ],

]
Add Database Column

In this step first we have to create migration for add provider and github_id in your user table. So let's run bellow command:

php artisan make:migration add_provider_and_github_id_column
<php


use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;


class AddProviderAndGithubIdColumn extends Migration

{

    /**

     * Run the migrations.

     *

     * @return void

     */

    public function up()

    {

        Schema::table('users', function ($table) {

            $table->string('provider');

            $table->string('github_id');

        });

    }


    /**

     * Reverse the migrations.

     *

     * @return void

     */

    public function down()

    {

        //

    }

}
app/User.php

Update mode like this way:

<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', 'provider', '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',

    ];

}
Create Routes

After adding provider and github_id column first we have to add new route for github login. so let's add bellow route in routes.php file.

app/Http/routes.php

Route::get('/auth/redirect/{provider}', '[email protected]');

Route::get('/callback/{provider}', '[email protected]');
Create SocialController

We need to create a controller name SocialController. Use the below command and create Controller :

php artisan make:controller SocialController
SocialController.php

Put bellow code on your SocialController.php file.

<php

 

namespace App\Http\Controllers;

 

use Illuminate\Http\Request;

use Validator,Redirect,Response,File;

use Socialite;

use App\User;


class SocialController extends Controller

{

    public function redirect($provider)

    {

        return Socialite::driver($provider)->redirect();

    }

 

    public function callback($provider)

    {

        $getInfo = Socialite::driver($provider)->user();

         

        $user = $this->createUser($getInfo,$provider);

 

        auth()->login($user);

 

        return redirect()->to('/home');

    }


    function createUser($getInfo,$provider){

 

        $user = User::where('github_id', $getInfo->id)->first();

        if (!$user) {

             $user = User::create([

                'name'     => $getInfo->name,

                'email'    => $getInfo->email,

                'provider' => $provider,

                'github_id' => $getInfo->id

            ]);

        }


        return $user;

    }    

}
Resources/Views/Auth/register.blade.php
<div class="form-group row mb-0">

     <div class="col-md-8 offset-md-4">

        <a href="{{ url('/auth/redirect/github') }}" class="btn btn-primary"><i class="fa fa-github"></i> Github</a>

    </div>

</div>
Resources/Views/Auth/login.blade.php
<div class="form-group row mb-0">

     <div class="col-md-8 offset-md-4">

       <a href="{{ url('/auth/redirect/github') }}" class="btn btn-primary"><i class="fa fa-github"></i> Github</a>

    </div>

</div>

It will help you....

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

🌤️ A wrapper around Open Weather Map API (Current weather)

Installation

You can install the package via composer:

source-shell
composer require gnahotelsolutions/laravel-weather
Usage
text-html-php
$weather = new Weather();

// Checking weather by city name
$currentWeatherInGirona = $weather->get('girona,es');

// You can use the city id, this will get you unambiguous results
$currentWeatherInGirona = $weather->find('3121456');

Units

By default the package uses metric for Celsius temperature results, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inUnits('imperial')->get('girona,es');

Language

By default the package uses es for the description translation, this can be modified using the configuration file or on the fly:

text-html-php
$weather = new Weather();

$currentWeatherInGirona = $weather->inLanguage('en')->get('girona');

Guzzle Client Instance

If you need to use another instance of Guzzle, to modify headers for example:

text-html-php
$weather = new Weather();

$guzzle = $this->getSpecialGuzzleClient();

$currentWeatherInGirona = $weather->using($guzzle)->get('girona');

Testing

source-shell
composer test

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Laravel Sweet Alert | How to use the SweetAlert package in Laravel PHP

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message. This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.

Sweet Alert is a laravel wrapper around #sweetalert javascript library for showing alert or notification message.

This is a great package created by rashid, and this laravel package is very famous because it has tonnes of features you can choose from.