how to add social login in laravel 5.8

    By: Manu
    2 years ago

    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


    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

    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
    

    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.

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

    Paste above code and hit enter it will create a project "newBlog".

    then select project folder using

    cd newBlog
    

    Check here

    how to add socialite in laravel 5.8


    now we need to run some other commands such as

    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
    


    Installing Socialite


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

    composer require laravel/socialite
    

    It should look like this

    how to add socialite in laravel 5.8


    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'),
                ],
    
    Note : we need to define "GITHUB_CLIENT_ID","GITHUB_CLIENT_SECRET" and "GITHUB_CALLBACK" in ".env" file

    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


        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);
                }
            }
    

    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.


        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');