2018-10-04 19:10:43 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2019-05-22 03:09:36 +02:00
|
|
|
namespace Http\Controllers\Auth;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2019-04-19 04:58:40 +02:00
|
|
|
use App\Http\Controllers\BaseController;
|
2018-10-04 19:10:43 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-05-22 02:56:47 +02:00
|
|
|
use App\Jobs\Account\CreateAccount;
|
|
|
|
use App\Libraries\MultiDB;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Libraries\OAuth;
|
|
|
|
use App\Models\User;
|
2019-04-19 04:58:40 +02:00
|
|
|
use App\Transformers\UserTransformer;
|
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;
|
2019-04-19 04:58:40 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-12-02 11:42:06 +01:00
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
2018-10-04 19:10:43 +02:00
|
|
|
|
2019-04-19 04:58:40 +02:00
|
|
|
class LoginController extends BaseController
|
2018-10-04 19:10:43 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| 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
|
|
|
|
2019-04-19 04:58:40 +02:00
|
|
|
protected $entity_type = User::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = UserTransformer::class;
|
|
|
|
|
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()
|
|
|
|
{
|
2019-04-19 04:58:40 +02:00
|
|
|
parent::__construct();
|
|
|
|
// $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
|
|
|
}
|
|
|
|
|
2019-04-19 03:59:07 +02:00
|
|
|
public function apiLogin(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateLogin($request);
|
|
|
|
|
2019-04-19 09:59:48 +02:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
$this->fireLockoutEvent($request);
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Too many login attempts, you are being throttled']);
|
|
|
|
}
|
|
|
|
|
2019-04-19 04:58:40 +02:00
|
|
|
if ($this->attemptLogin($request))
|
|
|
|
return $this->itemResponse($this->guard()->user());
|
2019-04-19 09:59:48 +02:00
|
|
|
else {
|
|
|
|
|
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
|
2019-04-19 04:58:40 +02:00
|
|
|
return response()->json(['message' => ctrans('texts.invalid_credentials')]);
|
2019-04-19 09:59:48 +02:00
|
|
|
}
|
2019-04-19 03:59:07 +02: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
|
|
|
{
|
2019-05-22 01:00:12 +02:00
|
|
|
if(request()->has('code'))
|
|
|
|
return $this->handleProviderCallback($provider);
|
|
|
|
else
|
|
|
|
return Socialite::driver($provider)->redirect();
|
2018-10-18 07:04:36 +02:00
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
{
|
2019-05-22 02:56:47 +02:00
|
|
|
//Auth::login($user, true);
|
|
|
|
return $this->itemResponse($user);
|
|
|
|
//return redirect($this->redirectTo); //todo return USERACCOUNT json
|
|
|
|
}
|
2019-05-22 03:00:23 +02:00
|
|
|
else if(MultiDB::checkUserEmailExists($socialite_user->getEmail()))
|
2019-05-22 02:56:47 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
return $this->errorResponse(['message'=>'User exists in system, but not with this authentication method'], 400);
|
|
|
|
|
|
|
|
}
|
|
|
|
/** 3. Automagically creating a new account here. */
|
|
|
|
else {
|
|
|
|
//todo
|
|
|
|
$name = OAuth::splitName($socialite_user->getName());
|
|
|
|
|
|
|
|
$new_account = [
|
|
|
|
'first_name' => $name[0],
|
|
|
|
'last_name' => $name[1],
|
|
|
|
'password' => '',
|
|
|
|
'email' => $socialite_user->getEmail(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$account = CreateAccount::dispatchNow($new_account);
|
|
|
|
|
2019-05-22 03:09:36 +02:00
|
|
|
return $this->itemResponse($account->default_company->owner());
|
2018-12-02 11:42:06 +01:00
|
|
|
}
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2018-10-18 07:04:36 +02:00
|
|
|
|
|
|
|
}
|
2018-10-04 19:10:43 +02:00
|
|
|
}
|