Skip to content

Instantly share code, notes, and snippets.

@austinkregel
Last active August 4, 2016 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinkregel/cceab4cfcd6c066b09616e9c095991ef to your computer and use it in GitHub Desktop.
Save austinkregel/cceab4cfcd6c066b09616e9c095991ef to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Models\User;
use Socialite;
class MagicController extends Controller
{
public function __construct(){
$this->socials = config('services.social');
}
/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback($provider)
{
if(!in_array($provider, $this->socials))
{
return view('errors.unsupported-provider');
}
$social_user = Socialite::driver($provider)->user();
// Find the user based upon the social id
$user = User::where('social_id', $social_user->id)->first();
// If the user is empty, they must not exist, so lets create them.
if (empty($user)) {
// If the provider doesn't offer and email for you to access *bitbucket*
// Use their id instead
if (empty($social_user->email)) {
$email = $social_user->id;
} else {
$email = $social_user->email;
}
// Create the user
$user = User::create([
'remember_token' => $social_user->token,
'email' => $email,
'name' => $social_user->name,
]);
}
auth()->loginUsingId($user->id);
// Redirect the user to the home route
return redirect('home');
}
public function loginPage()
{
// A view with the buttons to click on providers.
return view('auth.login', ['socials' => $this->socials);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment