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
Let’s start integrating Login with Facebook in the Laravel application. Enter inside the project: Go to the .env file, define the database name, user name, and password of your database. Install jetstream with following command: Execute command to generate authentication templates such as login, register and email verification.
To Start with follow the below steps. Install Laravel Socialite Package using composer. NOTE: If Using Laravel lower than version 8 Include Package in Providers. Include Facebook Service in config/services.php file. Here, you have to create Facebook app so that you can get a client_id and client_secret from Facebook.
Install socialite package in Laravel with the following command. Open config/app.php, register socialite plugin in providers, and aliases array. .... .... 'providers' => [ .... ....
When a user signs up with one social provider such as Facebook and then signs up with a different provider such as GitHub the next time, we shouldn’t end up with two different users on the site. I should be recognized as the same user provided I am using the same email across different social platforms.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With