how to add social login in laravel 5.8

HOW TO ADD SOCIALITE IN LARAVEL 5.8

how to add social login in laravel 5.8



Laravel is awesome framework. It makes things easier for us. Today we will install GitHub login system in our web app. And it is easy so lets see what outcome you will see.





How to add login with Github laravel 5
[ads]



Creating new project in laravel

So to start we must have a laravel project. To create a laravel project you need to follow this guide here





Note : PHP version should be 7.1.3 or latest for running laravel.



To create a new project in laravel we need to have xampp installed in our local server setup. In we need to select our htdocs folder and run create project command there. Commands for terminal given below
[ads]





cd..    //takes us one level up
cd   //change directory
cd htdocs //will take you to htdocs directory
ls  // will indicate your position in directory (current directory status)
sudo chmod -R 777 directoryname  //will allow read-write-delete full permission 

[ads]
here is the example how to use commands to get to directory
How to add login with Github laravel 5



In above image we are getting one level up using cd ..
then cd Applications/XAMPP/htdocs to select htdocs directory





Now once we have directory selected we can run create project command. In below given command we are creating “newBlog” project laravel.
[ads]



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

[ads]
Paste above code and hit enter it will create a project “newBlog”.
then select project folder using





cd newBlog

[ads]
Check here
how to add socialite in laravel 5.8



now we need to run some other commands such as
[ads]





php artisan make:auth    //to enable authentication login and register pages

And install node js from nodejs.org
after installing nodejs run below given command to install dependencies such as Bootstarp , JQuery etc in project.



npm install

[ads]





Installing Socialite

To install social lite. We need to run below given command



     composer require laravel/socialite

[ads]
It should look like this
how to add socialite in laravel 5.8
[ads]





Now we need to add credentials. For that we need to edit config/services.php file.



 'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => env('GITHUB_CALLBACK'),
    ],

[ads]





Note : we need to define “GITHUB_CLIENT_ID”,”GITHUB_CLIENT_SECRET” and “GITHUB_CALLBACK” in “.env” file



[ads]
We need to get client id and client secret from github website for that check here





how to add socialite in laravel 5.8



Now we need to setup functions in LoginController.php
[ads]





    public function redirectToProvider()
    {
        return Socialite::driver('github')->redirect();
    }

    
    public function handleProviderCallback()
    {
        $githubUser = Socialite::driver('github')->stateless()->user();

        if ((User::where('email', '=', $githubUser->getEmail()))->count() > 0){   //if email already in database
                $userHandle = new User;
                $result =User::whereIn('name', array($githubUser->getNickname()))->get();  
                $user = User::find($result[0]['id']);   //getting user id
                Auth::login($user);   //login user using id
                return redirect()->back(); //redirecting back
            }else{
                $userHandle = new User;    
                $userHandle->githubid=$githubUser->getId();
                $userHandle->name = $githubUser->getNickname();
                $userHandle->email = $githubUser->getEmail();
                $userHandle->githubAvatar = $githubUser->getAvatar();
                $userHandle->created_at = now();
                $userHandle->updated_at = now();
                Auth::login($userHandle,true);
            }
    }

[ads]
In above code we are using “User” model to communicate with database and inserting id,name,email and avatar into database.We are checking if email of user already in database or not.



Here is the up function from migrations folder “create user table” file. This is responsible for creating table in database.
[ads]





public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('githubid')->default(0);
            $table->string('name');
            $table->string('email');//->unique()
            $table->string('password')->nullable();
            $table->string('role')->default(0);
            $table->string('githubAvatar')->default(0);
            $table->rememberToken();
            $table->timestamps();
        });
    }

Its time to define routes



      Route::get('login/github', 'Auth\LoginController@redirectToProvider');
      Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback');

[ads]





https://18.191.183.98/2019/05/26/how-to-install-php-7-3-on-mac-updated-2019/