Search code examples
phplaravellaravel-socialite

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


I am trying to login using social sites in Laravel 5.1. I have installed socialite and following below tutorial:

https://mattstauffer.co/blog/using-github-authentication-for-login-with-laravel-socialite

http://tutsnare.com/social-authentication-in-laravel-5/

I have followed every step, when try to login using Github, it redirect to Github and after login it redirect to URL callback, but my problem is it won't store user data in my database.

Update-1

Now my problem is how to authenticate user using social network. Following is my code but it throw an error saying undefined index password. That error because Auth::attempt() has passsword parameter.

if (Auth::attempt(['email' => $email, 'user_id' => $user_id])) {
  return redirect()->intended('user/UserDashboard');
} else {
  here i am going to insert if user not logged in already
}

Update-2

After some research i realized that i need to create Custom Authentication Drivers. If any one have idea please post your answers

same question asked one year ago

laravel login with google account


Solution

  • 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