In this post, I would like to show you, how we can use Laravel’s Socialite plugin to use Twitter authentication in your Laravel based application.
To use twitter authentication APIs we need
client ID and
client secret and we can get these details from twitter app center. So lets login to https://apps.twitter.com/ and create a new app.
After creating the application you can get
client ID and
client secret from
Keys and Access Tokens tab.
Laravel application setup
Let’s create a brand new Laravel project with the following command:
Database connection settings
After creating project open
.env file and update your database credentials:
Next step, would be, go to the project root and open the terminal and type the following command. It will create the two tables, which is by default ships by Laravel 5.6
You might get following error :
to fix this, all you have to do is to edit your
AppServiceProvider.php file and add to the boot method a default string length:
Lets add two new columns called provider_id, provder to the users table, create a migration file with below commnad
Open create migration file and update it as shown below
NOTE: Changing columns for table “users” requires Doctrine DBAL; so install “doctrine/dbal” with composer
Again run the
php artisan migrate command from the project root. It will add new columns to the user table and it will modify email and password default values to null.
Now update your user model fillable array as shown below, with new columns
Installation of socialite
To get started with Socialite, use Composer to add the package to your project’s dependencies:
Now your twitter application credentials should be placed in your
config/services.php configuration file. (In real app place config values in .env file, its a good practice).
so let’s define callback and redirect routes in
Create a controller called
SocialAuthController with following artisan command
Now update your controller with below code:
Now update view file welcome.blade with below code:
That’s it, now start the application with
php artisan serve, and access your application
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.