Skip to content

Instantly share code, notes, and snippets.

@kadnan
Created December 8, 2016 10:49
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 kadnan/0dc01267d7a408359875c10da4f1bdb9 to your computer and use it in GitHub Desktop.
Save kadnan/0dc01267d7a408359875c10da4f1bdb9 to your computer and use it in GitHub Desktop.
Register a Social user manually
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use League\Flysystem\Exception;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Socialite;
use Illuminate\Support\Facades\Auth;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
//$this->middleware('auth');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
try {
$social_user = Socialite::driver('facebook')->user();
} catch (Exception $e) {
return redirect('/');
}
$user = User::where('social_id', $social_user->getId())->first();
if (!$user) {
User::create([
'social_id' => $social_user->getId(),
'name' => $social_user->getName(),
'email' => $social_user->getEmail(),
'social_type' => 'facebook',
]);
//auth()->login($user);
//Auth::loginUsingId(2, true);
//$this->guard()->login($user);
auth()->login($user);
Auth::login($user);
}
if ($this->guard()->check()) {
dd('User Logged IN');
} else {
dd('Nog loggine');
}
//return redirect('/home');
//return $user->getEmail();
// $user->token;
}
}
@kadnan
Copy link
Author

kadnan commented Dec 8, 2016

$user = User::where('social_id', $social_user->getId())->first();

        if (!$user) {
            User::create([
                'social_id' => $social_user->getId(),
                'name' => $social_user->getName(),
                'email' => $social_user->getEmail(),
                'social_type' => 'facebook',
            ]);
            
        }
        auth()->login($user); // SHOULD BE out of IF so control should go it anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment