Using Twitter Authentication For Login in Laravel 5

Previously, we dealt with Facebook Authentication.

Let’s see how we can utilize the Socialite package for Twitter Authentication:

1. Do a clean install of Laravel 5

2. Install Socialite

3. Head over to your config/app.php and add the following in the providers array:

4. Still in your config/app.php file, also add this to the aliases array:

We just registered a Facade for Laravel Socialite.

5. Create Twitter app.

Head over to  .  Click on create new app.

I named mine “Laravel Auth”.


Screen Shot 2015-08-24 at 5.20.09 PM


Now, Let’s edit something. Twitter won’t allow us to use localhost so we have to fake it, let’s add this as the callback URL.

For you to be able to use this URL on your machine, you’ll have to run your Laravel app using Vagrant and homestead. You can get how to install them here on Mac and here on Windows.

The Website URL can be any placeholder url..if you don’t have a real URL.

6. Add Twitter Config

Head over to your config/services.php and add this:

Open your .env file and add your twitter client id, secret and callback URL like so:

where xxxxxxx refers to your Consumer Key and Consumer secret.

7. Set up Routes and Controller Methods

You will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication.

Add these to routes.php

Add the following fields to the fillable array in the User model to make this fields mass assignable.Screen Shot 2015-08-24 at 8.03.44 PM

Let’s Open up our AuthController.php file in Auth folder and add this:

Note: Add the Socialite and Auth class reference at the top of this file like so:

Screen Shot 2015-08-24 at 6.12.30 AM

First, it redirects to Twitter, grabs the relevant information and then looks up the user and authenticate as that user. If the user doesn’t exist, it creates the user and authenticate.

8. Database

We need to update our users migration so that it will allow us to store twitter-specific information. Since this is a new app, I could just modify the users migration, but if you have an existing app, you’ll need to make a new migration.

Twitter API doesn’t return emails. If you need to really get the email of a user, you need to add your app to the set of whitelisted apps. Follow this link for more information on that. So let’s get a unique value which is the handle of the person.

Screen Shot 2015-08-24 at 8.02.06 PM

Now let’s get to work.

Head over to your welcome.php and edit it to have a login twitter button like so:

Screen Shot 2015-08-24 at 6.14.22 PM

Don’t forget to link the file with bootstrap.

In your routes.php, add this:

Create home.blade.php

Copy the contents of welcome.blade.php into it and just update the body content to have this:

Now, run your migrations lest we forget like so:

Are you ready to test your app?..after all the work!

Now, run php artisan serve and click on the Login with Twitter button on your homepage

Screen Shot 2015-08-24 at 7.52.08 PM


Screen Shot 2015-08-24 at 7.58.40 PM

Yaaay!!!..Now we are logged in from twitter..and we can access our data from Twitter.

You can check how to use Facebook authentication for Login in Laravel 5 here

You can decide to use the data for anything,…this tutorial aims to simply show you how to authenticate with twitter.

You can find the full source code of this tutorial here

Please, if you have any questions or observations, let me know in the comments section!



Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.