diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 8a9bacb7f8db..d747e7bcbba7 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -13,6 +13,8 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\BaseController; use App\Http\Controllers\Controller; +use App\Jobs\Account\CreateAccount; +use App\Libraries\MultiDB; use App\Libraries\OAuth; use App\Models\User; use App\Transformers\UserTransformer; @@ -118,12 +120,33 @@ class LoginController extends BaseController if($user = OAuth::handleAuth($socialite_user, $provider)) { - Auth::login($user, true); - - return redirect($this->redirectTo); //todo return USERACCOUNT json + //Auth::login($user, true); + return $this->itemResponse($user); + //return redirect($this->redirectTo); //todo return USERACCOUNT json + } + else if(MultiDB::checkUserEmailExists($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()); } - //throw error } } diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 842e445f9df8..18efb7fdad8b 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -81,7 +81,7 @@ class MultiDB } - return false; + return null; } public static function findAndSetDb($token) :bool diff --git a/app/Libraries/OAuth.php b/app/Libraries/OAuth.php index a645e9753a28..2256ad55a08c 100644 --- a/app/Libraries/OAuth.php +++ b/app/Libraries/OAuth.php @@ -36,7 +36,7 @@ class OAuth * @param Socialite $user */ - public static function handleAuth(object $user, string $provider) : ?User + public static function handleAuth(object $user, string $provider) { /** 1. Ensure user arrives on the correct provider **/ @@ -49,24 +49,19 @@ class OAuth { return $user; } + else + return false; - /** 2. If email exists, then they already have an account did they select the wrong provider? redirect to a guest error screen */ - - if(MultiDB::checkUserEmailExists($user->getEmail())) - { - Session::flash('error', 'User exists in system, but not with this authentication method'); //todo add translations - return view('auth.login'); - } - - /* - - Session::flash('error', 'User does not exist'); //todo add translations - return view('auth.login'); - */ - - /** 3. We will not handle automagically creating a new account here. */ + } + /* Splits a socialite user name into first and last names */ + public static function splitName($name) + { + $name = trim($name); + $last_name = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name); + $first_name = trim(preg_replace('#' . preg_quote($last_name, '/') . '#', '', $name)); + return [$first_name, $last_name]; } public static function providerToString(int $social_provider) : string diff --git a/app/Models/User.php b/app/Models/User.php index 3f414d79508d..bdde390dd27b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -112,7 +112,7 @@ class User extends Authenticatable implements MustVerifyEmail */ public function companies() { - return $this->belongsToMany(Company::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked'); + return $this->belongsToMany(Company::class)->using(CompanyUser::class)->withPivot('permissions', 'settings', 'is_admin', 'is_owner', 'is_locked'); } /** @@ -129,6 +129,7 @@ class User extends Authenticatable implements MustVerifyEmail * Returns the pivot tables for Company / User * * @return Collection + * */ public function user_companies() { diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index 73e76870b45c..8be4a28c3e16 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -14,9 +14,11 @@ namespace App\Transformers; use App\Models\Account; use App\Models\Company; use App\Models\CompanyToken; +use App\Models\CompanyUser; use App\Models\User; use App\Transformers\CompanyTokenTransformer; use App\Transformers\CompanyTransformer; +use App\Transformers\CompanyUserTransformer; use App\Utils\Traits\MakesHash; /** @@ -58,8 +60,8 @@ class UserTransformer extends EntityTransformer * @var array */ protected $availableIncludes = [ - 'user_company', 'companies', + 'company_tokens', ]; @@ -82,26 +84,37 @@ class UserTransformer extends EntityTransformer public function includeUserCompany(User $user) { +//cannot use this here as it will fail retrieving the company as we depend on the token in the header which may not be present for this request + //$transformer = new CompanyUserTransformer($this->serializer); - $transformer = new UserCompanyTransformer($this->serializer); - - return $this->includeItem($user->user_company(), $transformer, CompanyUser::class); + //return $this->includeItem($user->user_company(), $transformer, CompanyUser::class); } public function includeCompanies(User $user) { + $transformer = new CompanyTransformer($this->serializer); - return $this->includeCollection($user->companies(), $transformer, Company::class); + return $this->includeCollection($user->companies, $transformer, Company::class); + } public function includeCompanyToken(User $user) { + $transformer = new CompanyTokenTransformer($this->serializer); return $this->includeItem($user->token(), $transformer, CompanyToken::class); + } + + public function includeCompanyTokens(User $user) + { + + $transformer = new CompanyTokenTransformer($this->serializer); + + return $this->includeCollection($user->tokens, $transformer, CompanyToken::class); } }