The answer is no, not really lol. I just used LoginController to control all the Socialite stuff, updated my routes, job done.
Integrating Socialite with Spark
Hi all,
I've got Socialite working on my "standard" Laravel App. I'm just wondering if the way to integrate Socialite into a Spark based App is exactly the same process? I've noticed that Spark uses a different AuthController file which I think is called LoginController. Apart from that, is there much different?
Cheers, Andy
Just curious... So you can use Socialite with Spark? I'm just doing research on possibly buying spark to use with my application and social log ins are a must.
Which methods did you override/what was needed to implement Socialite into Spark?
@andyjh07 I'm in the process of doing this as well, can you provide more detail?
From above my best guess is;
- Add the methods found in the basic usage section of socialite, you placed on your
LoginController, - Update your routes
- Update your views with a login/register link/button pointing to their respective route
Sorry all, have been on holiday in Portugal. I'll go through my files tonight but not a lot is actually different to how you would usually implement Socialite to a Laravel app.
I can confirm that Socialite works with Spark, quite nicely too.
Andy
@andyjh07 I'd love to see a step-by-step on how you have it integrated. Specifically how you are handling keeping Spark updated, as the controller in question is part of the core Spark files, or not?
Mine is below:
Implementing Socialite with Spark
New Files created
-
app/Http/Controllers/SocialAuthController.php -
app/SocialAccount.php -
app/SocialAccountService.php -
database/migrations/{TIMESTAMP}_create_social_accounts_table.php
Exiting Files Updated
-
app/Http/routes.php -
config/app.php -
config/services.php - View of your choice to add the social login buttons
Overall the process was straight forward as I have already integrated Socialite with Laravel using an unofficial, but thorough login with Facebook guide. Note you can skip to the Twitter guide as this guide does not have the Facebook provider hard coded, i.e., social login providers are dynamic based on the URI.
The key to keep in mind is that Socialite handles the Social login API/SDK, however it does not handle creating a user in our app. That is upto us.
The steps are as followed:
- Install Socialite via composer
- Create a table that links our socially logged in user with a user in the users table
- Create a model linking our users table to the social table
- Create a controller to handle the login/register request
- Create a new service to handle user creation and login
- Update our routes
Reference
@zanematthew Could well be a better way of doing it than my way. I just tied it into the already existing LoginController but honestly didn't think about the impact of updates. The rest of what I did is incredibly similar to your own except I just put the token for Facebook and Twitter into a column on my Users table.
Did you just make your Socialite routes point to your new SocialAuthController and handle all the Socialite stuff there? Seems like a much cleaner way of doing it ;)
A lot of my spark work is experimentation at the moment as I couldn't find any good docs on integrating Socialite properly or creating additional registration fields, but Taylor has created a little cookbook for that bit now ;) haha.
@andyjh07 thanks. My routes do point to the SocialAuthController, the {provider} is dynamic, based on github, or facebook.
$router->get('/redirect/{provider}', 'SocialAuthController@redirect');
$router->get('/callback/{provider}', 'SocialAuthController@callback');
I'm with you on the Spark work being experimental. I've been playing with Laravel off and on since January, and since the release of Spark, I've embraced it. Excited to launch my first personal app in Laravel/Spark this summer.
If you want to handle oauth2 users the same way as normaly registered users (free trial end date calculation etc.) I recommend using Laravel\Spark\Interactions\Auth\Register class. The class expects Laravel\Spark\Contracts\Http\Requests\Auth\RegisterRequest interface as a parameter. That would be a BraintreeRegisterRequest or SparkRegisterRequest. So I did it like this in my create user function which receives Socialite's ProviderUser object as a parameter:
/* if user doesn't exist */
if (!$user) {
$attributes = ['email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'photo_url' => $providerUser->getAvatar(),
'facebook_id' => $providerUser->getId(),
'password' => '',
'busy' => true,
'facebook_id' => $providerUser->getId()
];
// create a BraintreeRegisterRequest (or SparkRegisterRequest) with user registration data
$request = Spark\Http\Requests\Auth\BraintreeRegisterRequest::create('', 'GET', $attributes);
// User Register object to handle user creation
$user = (new Register())->handle($request); // voi-la! that's your new user
}
@simber thanks for that. I'm doing an implementation with league/oauth2-client due to limitations with refresh tokens in socialite.
Your code doesn't work with Teams enabled, do you have any ideas how to rectify that? I would prefer the user to create the team after social auth takes place.
I followed the instructions here for basic implementation: https://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html
Without touching LoginController. But needed to adjust original Spark schema to allow for nullable password field. (Still need to check the security of this.)
Added avatars by including photo_url in the User model:
protected $fillable = [
'name',
'email',
'photo_url',
];
Then followed instructions here http://stackoverflow.com/questions/29009457/laravel-socialite-avatar-is-to-slow for getting a larger avatar image:
protected function getAvatarUrl($providerName, $providerUser)
{
$avatar = '';
if ($providerName == 'FacebookProvider')
{
// Facebook avatar URL
$avatarUrl = $providerUser->avatar_original;
}
else
{
// Google avatar URL
$avatarUrl = preg_replace('/\?sz=[\d]*$/', '', $providerUser->avatar);
}
return $avatarUrl;
}
Then add the avatar URL with other user details:
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'photo_url' => $avatar,
]);
I am replying to the old thread. Hope to get a reply.
Why do you need social_accounts table? Can't we create the user with the email ID and log them in with the email ID which we get from the callback?
Please or to participate in this conversation.