How to login using Github, Facebook, Gmail and Twitter in Laravel 5.1?

Well the very first thing that I assume is that you have ran php artisan migrate(And that may be your main problem).

Next the problem that you have while storing data to database, do you have a working connection to your database from that Class \App\User? If you have named it something different I request you to change that part after the first \App\[YourClassName].

Alright if that's working properly, you might have problem with getting data back. Just try doing somewhat like this in some different route:

<?php
    //Routes.php

    Route::get('trygithub', function(){
        $user = Socialize::with('github')->user()
        dd($user);
    });

And if you get some output on the screen, well that's OK. If you do not get anything, try to check your github configurations.

And yeah just one more thing... Still if you do not get desired output, try to comment below. But I am pretty much sure that this ones will definitely work.

Update-1 answer:

If you want to attempt login for your user, and you are using any social profile like let's say FB, you need to go like this: https://maxoffsky.com/word/wp-content/uploads/2013/08/Social-Logon.png

And that's not just one case of FB, you get some specific codes like in Google, you get one refresh token code and in twitter a permanent access token and secret. So just try to go like that. Or, have one specific term as a password for your user. str_random() may be helpful in that way and I strongly recommend that instead of these two.

Update-2 answer:

I do not recommend going through this because you will not get anything by reinventing the wheel. But still if you want to go through that nasty process, here's the link.

http://laravel.com/docs/5.1/authentication#adding-custom-authentication-drivers


It's up to you to manage that.

You can find an interesting discussion that shows a few ways to do what you need.

Once you get redirected from Github you should be able to access the details like so.

 $user->getId();
 $user->getNickname();
 $user->getName();
 $user->getEmail();
 $user->getAvatar();`

$user->getId() would be the unique id returned from Github.

Save this field somewhere in your database. If you want to add them to your users table you could do something like this:

$newUser = new \App\User;
$newUser->name=$user->getName();
//Or use the nickname
//$newUser->name=$user->getNickname();
$newUser->email=$user->getEmail();
$newUser->social_id=$user->getId();
$newUser->save();

I'm not recommending this as the way to do it, but it should give you enough of an example to make it work.

When someone tries to login with Github, if that ID doesn't exist, either create a new account for them or throw an error.

Just remember, the ID returned is from Github not your webapp. So your user would have an "id" for your web app and a "github_id" that you store so you know what user they belong to.


The idea is to keep a table for each user's connected accounts. The table will contain the user id, social account type and social account id.

User => has Many "social accounts"

social account => has one "User"

When a user logs in with a social account we will search for the social account with the social account id and type. get the user object and login the user immediately.

If a user is not found and if the social network api returns the user's email, we will get the user by that email and login the user, also connect the new social account to the user object. However i do not recommend this, because it is a security issue, if someone changed their social account email to an existing user's email in your app, he can gain access to the account easily.

If all above fails we need to direct the user to the signup screen, with the email we already have from the social network api (if available) and the social account type, that if we need to connect after successful signup.