2018-10-04 19:10:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Libraries\OAuth;
|
|
|
|
use App\Models\User;
|
2018-11-02 11:54:46 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-10-04 19:10:43 +02:00
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
2018-11-02 11:54:46 +01:00
|
|
|
use Illuminate\Http\Request;
|
2018-12-02 11:42:06 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles authenticating users for the application and
|
|
|
|
| redirecting them to your home screen. The controller uses a trait
|
|
|
|
| to conveniently provide its functionality to your applications.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesUsers;
|
2018-11-02 11:54:46 +01:00
|
|
|
use UserSessionAttributes;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Where to redirect users after login.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-10-17 14:26:27 +02:00
|
|
|
protected $redirectTo = '/dashboard';
|
2018-10-04 19:10:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-10-15 07:00:48 +02:00
|
|
|
$this->middleware('guest:user')->except('logout');
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|
2018-10-18 07:04:36 +02:00
|
|
|
|
2018-11-20 05:36:56 +01:00
|
|
|
/**
|
|
|
|
* Once the user is authenticated, we need to set
|
|
|
|
* the default company into a session variable
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
public function authenticated(Request $request, User $user) : void
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2019-03-26 12:31:07 +01:00
|
|
|
//$this->setCurrentCompanyId($user->companies()->first()->account->default_company_id);
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 05:36:56 +01:00
|
|
|
/**
|
|
|
|
* Redirect the user to the provider authentication page
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
public function redirectToProvider(string $provider)
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
|
|
|
return Socialite::driver($provider)->redirect();
|
|
|
|
}
|
|
|
|
|
2018-11-20 05:36:56 +01:00
|
|
|
/**
|
|
|
|
* Received the returning object from the provider
|
|
|
|
* which we will use to resolve the user
|
|
|
|
*
|
|
|
|
* @return redirect
|
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
public function handleProviderCallback(string $provider)
|
2018-10-18 07:04:36 +02:00
|
|
|
{
|
2018-12-02 11:42:06 +01:00
|
|
|
$socialite_user = Socialite::driver($provider)->user();
|
|
|
|
|
|
|
|
if($user = OAuth::handleAuth($socialite_user, $provider))
|
|
|
|
{
|
|
|
|
Auth::login($user, true);
|
|
|
|
|
2019-04-18 08:11:37 +02:00
|
|
|
return redirect($this->redirectTo); //todo return USERACCOUNT json
|
2018-12-02 11:42:06 +01:00
|
|
|
}
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2018-12-02 11:42:06 +01:00
|
|
|
//throw error
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
}
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|