8

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

Community
  • 1
  • 1
scott
  • 3,112
  • 19
  • 52
  • 90
  • Could you share some code? You should be able to use the Auth facade to check if the user is logged in. http://laravel.com/docs/master/authentication#retrieving-the-authenticated-user – arikin Aug 17 '15 at 03:43

3 Answers3

7

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

Community
  • 1
  • 1
Aditya Giri
  • 1,786
  • 18
  • 33
  • As far what I am concerned, you shouldn't have a custom authentication driver installed to get this thing working. Even I will be updating my answer to answer both of your updates. – Aditya Giri Aug 28 '15 at 11:46
  • And @tester here's it – Aditya Giri Aug 28 '15 at 11:58
  • .for update one you have explained nicely.for second update you have given me a link.that i already refered but didnt got it how it works.is it possible post how to do in my case – scott Aug 28 '15 at 12:02
  • @ Aditya Giri,i am facing problem in yes or no.Means how to authenticate .In normal php we can use session to authenticate but here in laravel we usually use Auth to authenticate ,But for authenticate usualy it take email and password.If we use socialite then we get only email id,token ,id but not password – scott Aug 28 '15 at 12:07
  • As I stated above, just add their email id. And as a password you may use a `str_random()` and I recommend using `Auth::id()` for auth driver – Aditya Giri Aug 28 '15 at 12:34
  • And yeah I will try to post how to do that in your case @tester – Aditya Giri Aug 28 '15 at 12:35
  • @Aditya.now i am facing one problem – scott Aug 31 '15 at 12:36
  • can you check this question http://stackoverflow.com/questions/32306732/laravel-auth-not-working – scott Aug 31 '15 at 12:36
2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jonny C
  • 1,943
  • 3
  • 20
  • 36
2

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.

Community
  • 1
  • 1
astroanu
  • 3,901
  • 2
  • 36
  • 50
  • @astroanu.thank you for the answer.is it possible post some code – scott Aug 27 '15 at 07:10
  • it's a little bit broad question to answer. i have successfully done this with lusitanian/oauth package. might do a tutorial in the future. – astroanu Aug 27 '15 at 09:05