sarga/packages/Webkul/SocialLogin/src/Http/Controllers/LoginController.php

83 lines
2.3 KiB
PHP
Raw Normal View History

2020-06-26 13:39:53 +00:00
<?php
namespace Webkul\SocialLogin\Http\Controllers;
use Illuminate\Routing\Controller;
2020-09-07 06:00:47 +00:00
use Illuminate\Support\Facades\Event;
use Laravel\Socialite\Facades\Socialite;
2020-06-26 13:39:53 +00:00
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Webkul\SocialLogin\Repositories\CustomerSocialAccountRepository;
class LoginController extends Controller
{
use DispatchesJobs, ValidatesRequests;
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* CustomerSocialAccountRepository
*
* @var \Webkul\SocialLogin\Repositories\CustomerSocialAccountRepository
*/
protected $customerSocialAccountRepository;
/**
* Create a new controller instance.
*
* @param \Webkul\SocialLogin\Repositories\CustomerSocialAccountRepository $customerSocialAccountRepository
* @return void
*/
public function __construct(CustomerSocialAccountRepository $customerSocialAccountRepository)
{
$this->customerSocialAccountRepository = $customerSocialAccountRepository;
$this->_config = request('_config');
}
/**
* Redirects to the social provider
*
* @param string $provider
* @return \Illuminate\Http\Response
*/
public function redirectToProvider($provider)
{
try {
return Socialite::driver($provider)->redirect();
} catch (\Exception $e) {
session()->flash('error', $e->getMessage());
2020-09-07 06:00:47 +00:00
return redirect()->route('customer.session.index');
}
2020-06-26 13:39:53 +00:00
}
2020-09-07 06:00:47 +00:00
2020-06-26 13:39:53 +00:00
/**
* Handles callback
*
* @param string $provider
* @return \Illuminate\Http\Response
*/
public function handleProviderCallback($provider)
{
try {
$user = Socialite::driver($provider)->user();
} catch (\Exception $e) {
return redirect()->route('customer.session.index');
}
2020-09-07 06:00:47 +00:00
2020-06-26 13:39:53 +00:00
$customer = $this->customerSocialAccountRepository->findOrCreateCustomer($user, $provider);
auth()->guard('customer')->login($customer, true);
2020-09-07 06:00:47 +00:00
// Event passed to prepare cart after login
Event::dispatch('customer.after.login', $customer->email);
2020-06-26 13:39:53 +00:00
return redirect()->intended(route($this->_config['redirect']));
}
}