mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
INA-12 | add apple to the login, refactor of microsoft login.
This commit is contained in:
parent
4884de4ec8
commit
117a4e4028
@ -250,7 +250,7 @@ class LoginController extends BaseController
|
||||
// $truth->setCompanyToken(CompanyToken::where('user_id', auth()->user()->id)->where('company_id', $user->account->default_company->id)->first());
|
||||
|
||||
/*On the hosted platform, only owners can login for free/pro accounts*/
|
||||
if(Ninja::isHosted() && !$cu->first()->is_owner && !$user->account->isEnterpriseClient())
|
||||
if (Ninja::isHosted() && !$cu->first()->is_owner && !$user->account->isEnterpriseClient())
|
||||
return response()->json(['message' => 'Pro / Free accounts only the owner can log in. Please upgrade'], 403);
|
||||
|
||||
event(new UserLoggedIn($user, $user->account->default_company, Ninja::eventVars($user->id)));
|
||||
@ -358,15 +358,22 @@ class LoginController extends BaseController
|
||||
*/
|
||||
public function oauthApiLogin()
|
||||
{
|
||||
|
||||
$message = 'Provider not supported';
|
||||
if (request()->input('provider') == 'google') {
|
||||
return $this->handleGoogleOauth();
|
||||
} elseif (request()->input('provider') == 'microsoft') {
|
||||
if (request()->has('token')) {
|
||||
return $this->handleMicrosoftOauth(request()->get('token'));
|
||||
return $this->handleSocialiteLogin('microsoft', request()->get('token'));
|
||||
} else {
|
||||
$message = 'Bearer token missing for the microsoft login';
|
||||
}
|
||||
} elseif (request()->input('provider') == 'apple') {
|
||||
if (request()->has('token')) {
|
||||
return $this->handleSocialiteLogin('apple', request()->get('token'));
|
||||
} else {
|
||||
$message = 'Token is missing for the apple login';
|
||||
}
|
||||
}
|
||||
|
||||
return response()
|
||||
@ -375,14 +382,30 @@ class LoginController extends BaseController
|
||||
->header('X-Api-Version', config('ninja.minimum_client_version'));
|
||||
}
|
||||
|
||||
private function handleMicrosoftOauth($token)
|
||||
private function getSocialiteUser(string $provider, string $token)
|
||||
{
|
||||
$user = Socialite::driver('microsoft')->userFromToken($token);
|
||||
return Socialite::driver($provider)->userFromToken($token);
|
||||
}
|
||||
|
||||
private function handleSocialiteLogin($provider, $token)
|
||||
{
|
||||
$user = $this->getSocialiteUser($provider, $token);
|
||||
if ($user) {
|
||||
return $this->loginOrCreateFromSocialite($user, $provider);
|
||||
}
|
||||
return response()
|
||||
->json(['message' => ctrans('texts.invalid_credentials')], 401)
|
||||
->header('X-App-Version', config('ninja.app_version'))
|
||||
->header('X-Api-Version', config('ninja.minimum_client_version'));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function loginOrCreateFromSocialite($user, $provider)
|
||||
{
|
||||
$query = [
|
||||
'oauth_user_id' => $user->id,
|
||||
'oauth_provider_id' => 'microsoft',
|
||||
'oauth_provider_id' => $provider,
|
||||
];
|
||||
if ($existing_user = MultiDB::hasUser($query)) {
|
||||
|
||||
@ -411,7 +434,7 @@ class LoginController extends BaseController
|
||||
|
||||
auth()->user()->update([
|
||||
'oauth_user_id' => $user->id,
|
||||
'oauth_provider_id' => 'microsoft',
|
||||
'oauth_provider_id' => $provider,
|
||||
]);
|
||||
|
||||
$cu = $this->hydrateCompanyUser();
|
||||
@ -424,7 +447,6 @@ class LoginController extends BaseController
|
||||
|
||||
return $this->timeConstrainedResponse($cu);
|
||||
}
|
||||
|
||||
$name = OAuth::splitName($user->name);
|
||||
|
||||
$new_account = [
|
||||
@ -433,7 +455,7 @@ class LoginController extends BaseController
|
||||
'password' => '',
|
||||
'email' => $user->email,
|
||||
'oauth_user_id' => $user->id,
|
||||
'oauth_provider_id' => 'microsoft',
|
||||
'oauth_provider_id' => $provider,
|
||||
];
|
||||
|
||||
MultiDB::setDefaultDatabase();
|
||||
@ -456,22 +478,14 @@ class LoginController extends BaseController
|
||||
}
|
||||
|
||||
|
||||
return response()
|
||||
->json(['message' => ctrans('texts.invalid_credentials')], 401)
|
||||
->header('X-App-Version', config('ninja.app_version'))
|
||||
->header('X-Api-Version', config('ninja.minimum_client_version'));
|
||||
|
||||
|
||||
}
|
||||
|
||||
private function hydrateCompanyUser() :Builder
|
||||
private function hydrateCompanyUser(): Builder
|
||||
{
|
||||
|
||||
$cu = CompanyUser::query()->where('user_id', auth()->user()->id);
|
||||
|
||||
if(CompanyUser::query()->where('user_id', auth()->user()->id)->where('company_id', auth()->user()->account->default_company_id)->exists())
|
||||
if (CompanyUser::query()->where('user_id', auth()->user()->id)->where('company_id', auth()->user()->account->default_company_id)->exists())
|
||||
$set_company = auth()->user()->account->default_company;
|
||||
else{
|
||||
else {
|
||||
$set_company = $cu->first()->company;
|
||||
}
|
||||
|
||||
@ -487,12 +501,11 @@ class LoginController extends BaseController
|
||||
if($cu->count() == 0)
|
||||
return $cu;
|
||||
|
||||
if(auth()->user()->company_users()->count() != auth()->user()->tokens()->distinct('company_id')->count())
|
||||
{
|
||||
if (auth()->user()->company_users()->count() != auth()->user()->tokens()->distinct('company_id')->count()) {
|
||||
|
||||
auth()->user()->companies->each(function($company){
|
||||
auth()->user()->companies->each(function ($company) {
|
||||
|
||||
if(!CompanyToken::where('user_id', auth()->user()->id)->where('company_id', $company->id)->exists()){
|
||||
if (!CompanyToken::where('user_id', auth()->user()->id)->where('company_id', $company->id)->exists()) {
|
||||
|
||||
CreateCompanyToken::dispatchNow($company, auth()->user(), "Google_O_Auth");
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user