How do I log into social media in laravel 8?

A few days back, I was going through the clients' problems and found that laravel login with google is one of the major concerns of many. People are facing a lot of trouble in integrating Google Login in Laravel.

Indeed, Google sign-in is a popular platform that is commonly used on social sites websites. When it comes to log in to social profiles on websites, then many of us skip the process of email validation. Are you one of them?

Well! With this step, rely on the social sites, and they always validate users, which further gives them a better experience as they don't need to remember the credentials to log in time and again on the website. Moreover, it also saves a lot of time in incorporating codes for verification. 

But to avail all such benefits, it is vital for you to log in to Google authentication in laravel. Don't know how? Need not fret! 

In this article, I am going to share the steps on how to log in with a google account using the socialite package in laravel app. By following the below-added steps, you can easily sign-in and signup with a Google Gmail account in laravel and can also use google authentication for login in laravel.

Steps for Google Authentication Login in Laravel 

Let's begin.. 

Step-1 Firstly, you need to check whether your system has a laravel project or not. If not, then you can download it by using the below-mentioned command. 

Run this command in Command Prompt:

“composer create-project --prefer-dist laravel/laravel googleLogin”

This command will help you to successfully download in your system. 

Step-2 Once you download a laravel project, it's time to install the socialite package. Are you thinking how? 

To download the socialite package you can run this command

"composer require laravel/socialite"

After downloading the socialite package, you need to add providers and aliases in the config file. Now open config/app.php file and incorporate the service provider and alias. Here is the screenshot of folder directories :

Now, you will see the file name with the app just inside the config folder.

After opening this file, paste the below-mentioned lines.

“'providers' => [

    Laravel\Socialite\SocialiteServiceProvider::class,

],

'aliases' => [

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

],"

You will see some text on your screen-

Step-3 Now, it's time to create a google client id and secret id. Say if you don't have a google app account, it's time to create one from here- Google Developers Console

You will get a screen similar to the below-added screenshot- 

Now just click on the credentials and choose OAuth client id.

Once you are successfully done with the auth id creation, you will get the client id and secret id.

Here, you have to mention your redirect URL just as mentioned in the below screenshot- 

Step-4 In this step, you have to create a migration for adding google_id in your user table. To proceed with this step, you can run the below command-

“php artisan make:migration add_google_id_column”

Here you can check the migration file in the database folder.

Now, open the migration file and paste the below-added code to proceed further-

“$table->string('google_id')->nullable();”

The below-added screenshot will help you perform the process with a better understanding. 

Now run this command- "php artisan migrate". After running this command your column (Google id) will be added to your database automatically, just like the below screenshot- 

Step-5 Once the google_id column is added, you have to incorporate a new route for google login. For this, add the below route in web.php file. Follow the screenshot to find the route file in your project. 

Now, just open the file and copy-paste the code as added below- 

“Route:get(‘google’,function(){

Return view(‘googleAuth’);

})

Route::get('auth/google', 'Auth\LoginController@redirectToGoogle');

Route::get('auth/google/callback', 'Auth\LoginController@handleGoogleCallback');”

Step-6 Now, you need to add a method of google authentication that will surely help you in handling google callback.

For this, put the below-added code in your LoginController.php file.

"app/Http/Controllers/Auth/LoginController.php"

You can check out the below screenshot to know where the controller is exactly located. 

Now, if you want to create a new controller, then you can use this command to complete the step- 

"Php artisan make:controller GoogleLogin"

Now, open the respective controller file and paste the code as added below- 

<?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

{

    use AuthenticatesUsers;

    public function __construct()

    {

        $this->middleware('guest')->except('logout');

    }

    public function redirectToGoogle()

    {

        return Socialite::driver('google')->redirect();

    }

    public function handleGoogleCallback()

    {

        try {

            $user = Socialite::driver('google')->user();

            $finduser = User::where('google_id', $user->id)->first();

            if($finduser){

                Auth::login($finduser);

                return return redirect('/home');

            }else{

                $newUser = User::create([

                    'name' => $user->name,

                    'email' => $user->email,

              'google_id'=> $user->id

                ]);

                Auth::login($newUser);

                return redirect()->back();

            }

        } catch (Exception $e) {

            return redirect('auth/google');

        }

    }

}

Step-7 Now, you are one step away from your target. In this last step, you need to add a blade view. For this, you just need to create a new file googleAuth.blade.php file by adding below added code:

resources/views/googleAuth.blade.php

You will get a view file in resource/view:

In the view folder you can create your view file- 

After creating view file paste this code:

<!DOCTYPE html>

<html>

<head>

    <title>Laravel 5.8 Login with Google Account Example</title>

</head>

<body>

    <div class="container">

       <div class="row">

       <div class="col-md-12 row-block">

<a href=” “class=”btn bth-lg-primaty btn-block ”>

          <strong>Login With Google</strong>

          </a> 

         </div>

       </div>

    </div>

</body>

</html>

I personally applied the google auth login in GPE( Guest Post Engine). For better understanding, I am sharing a screenshot and video.

Here you will see Google sign-in button- 

Now, if I'll click the button, it will redirect to the google sign page, as shown below.

Here you go! 

Points to Consider

Whenever you perform the above steps, make sure you keep the below-added points in mind-

1- There are chances that you might have to face trouble while redirecting your page to google. Say if you face some similar error as below, then You just need to change two lines of code in your controller.

Here is the code that you need to change-

“return Socialite::driver('google')->stateless()->redirect();”

2- Before you proceed, I recommend you re-check your redirect URL in the google developer console. It will help you to complete the task without any hurdle.

The Final Verdict

Congratulations folks! You have successfully logged in with google in laravel. Now you don't have to worry about remembering your passwords for different profiles. 

I hope the above-added steps, video, and images are clear and will surely help you hit your target. Still, if you need any help in website development and create professional website, feel free to contact us and get excellent responses. 

How do I log into Facebook using laravel?

Laravel 9 Socialite Login with Facebook Account Example.
Step 1: Install Laravel 9. ... .
Step 2: Install JetStream. ... .
Step 3: Install Socialite. ... .
Step 4: Create Facebook App. ... .
Step 5: Add Database Column. ... .
Step 6: Create Routes. ... .
Step 7: Create Controller. ... .
Step 8: Update Blade File..

How do I log into twitter using laravel?

Laravel 9 OAuth Login with Twitter Example.
Step 1: Install Laravel App..
Step 2: Make Database Connection..
Step 3: Install Laravel Livewire and Jetstream Packages..
Step 4: Setting Up Socialite Library..
Step 5: Update Social ID in Database..
Step 6: Add Twitter Client ID and Secret..
Step 7: Build Controller..
Step 8: Add Routes..

How do I login as user in laravel?

How to Setup Laravel Login Authentication in Simple and Easy Steps.
Prerequisites for Laravel 5.5 custom authentication:.
Setup the Database..
Setup the Routes..
Make the Controllers..
Setup the View..
Laravel login and registration Setup..
Database Migration..
Laravel 5.5 authentication..

How do I log into LinkedIn with laravel?

Laravel 9 Social Login with LinkedIn Example.
Step 1: Set Up Laravel Project..
Step 2: Make Database Connection..
Step 3: Install Jetstream Library..
Step 4: Configure Socialite Pacakage..
Step 5: Add and Migrate Linkedin Property in Users Table..
Step 6: Add Linkedin Client ID and Secret..
Step 7: Prepare Controller..

Postingan terbaru

LIHAT SEMUA