Skip to content

Commit

Permalink
Added basic User provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
EkiciLP committed Aug 14, 2024
1 parent bad5409 commit e42182f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions app/Http/Controllers/Auth/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Auth;

use App\Services\Users\UserCreationService;
use Illuminate\Auth\AuthManager;
use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\Facades\Socialite;
Expand All @@ -18,7 +19,8 @@ class OAuthController extends Controller
*/
public function __construct(
private AuthManager $auth,
private UserUpdateService $updateService
private UserUpdateService $updateService,
private UserCreationService $creationService
) {
}

Expand Down Expand Up @@ -47,15 +49,26 @@ protected function callback(Request $request, string $driver): RedirectResponse
return redirect()->route('account');
}

try {
$user = User::query()->whereJsonContains('oauth->'. $driver, $oauthUser->getId())->firstOrFail();
$user = User::query()->whereJsonContains('oauth->'. $driver, $oauthUser->getId())->first();

$this->auth->guard()->login($user, true);
} catch (Exception $e) {
// creating the user if none was found (User Provisioning)
if (!$user && env('OAUTH_USER_PROVISIONING') == 'true') {
$userdata = [
'username' => $oauthUser->getId(),
'email' => $oauthUser->getEmail(),
'name_first' => str_split($oauthUser->getName())[0],
'name_last' => str_split($oauthUser->getName())[1],
'oauth' => [$driver => $oauthUser->getId()],
];

$user = $this->creationService->handle($userdata);
}else {
// No user found - redirect to normal login
return redirect()->route('auth.login');
}

$this->auth->guard()->login($user, true);

return redirect('/');
}
}

0 comments on commit e42182f

Please sign in to comment.