1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Http/Controllers/Auth/LoginController.php

153 lines
4.3 KiB
PHP
Raw Normal View History

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
namespace App\Http\Controllers\Auth;
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;
use App\Jobs\Account\CreateAccount;
use App\Libraries\MultiDB;
use App\Libraries\OAuth;
use App\Models\User;
2019-04-19 04:58:40 +02:00
use App\Transformers\UserTransformer;
use App\Utils\Traits\UserSessionAttributes;
2018-10-04 19:10:43 +02:00
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
2019-04-19 04:58:40 +02:00
use Illuminate\Support\Facades\Log;
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;
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
*/
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
}
/**
* Once the user is authenticated, we need to set
* the default company into a session variable
*
* @return void
*/
public function authenticated(Request $request, User $user) : void
{
2019-03-26 12:31:07 +01:00
//$this->setCurrentCompanyId($user->companies()->first()->account->default_company_id);
}
2019-04-19 03:59:07 +02:00
public function apiLogin(Request $request)
{
$this->validateLogin($request);
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());
else {
$this->incrementLoginAttempts($request);
2019-04-19 04:58:40 +02:00
return response()->json(['message' => ctrans('texts.invalid_credentials')]);
}
2019-04-19 03:59:07 +02:00
}
/**
* Redirect the user to the provider authentication page
*
* @return void
*/
public function redirectToProvider(string $provider)
{
2019-05-22 01:00:12 +02:00
if(request()->has('code'))
return $this->handleProviderCallback($provider);
else
return Socialite::driver($provider)->redirect();
}
/**
* Received the returning object from the provider
* which we will use to resolve the user
*
* @return redirect
*/
public function handleProviderCallback(string $provider)
{
$socialite_user = Socialite::driver($provider)->user();
if($user = OAuth::handleAuth($socialite_user, $provider))
{
//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()))
{
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);
return $this->itemResponse($account->default_company->users->first());
}
}
2018-10-04 19:10:43 +02:00
}