Skip to content

Commit

Permalink
fix: filament config and auth
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbutcher committed Oct 12, 2023
1 parent 18f0990 commit 13b5c68
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 170 deletions.
12 changes: 12 additions & 0 deletions config/filament.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use JoelButcher\Socialstream\Features;
use JoelButcher\Socialstream\Providers;

return [
'middleware' => ['web'],
'prompt' => 'Or Login Via',
'providers' => [
// Providers::github(),
],
];
174 changes: 7 additions & 167 deletions src/Actions/Auth/Filament/AuthenticateOauthCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,177 +33,17 @@ public function __construct(

public function authenticate(string $provider, ProviderUser $providerAccount): Response|RedirectResponse
{
$account = Socialstream::findConnectedAccountForProviderAndId($provider, $providerAccount->getId());
$user = auth()->user();

// Authenticated...
if (! is_null($user = auth()->user())) {
return $this->alreadyAuthenticated($user, $account, $provider, $providerAccount);
if (! $user) {
$user = Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->first()
?? $this->createsUser->create($provider, $providerAccount);
}

// Registration...
$previousUrl = session()->get('socialstream.previous_url');
($account = Socialstream::findConnectedAccountForProviderAndId($provider, $providerAccount->getId()))
? $this->updatesConnectedAccounts->update($user, $account, $provider, $providerAccount)
: $this->createsConnectedAccounts->create($user, $provider, $providerAccount);

if (
! $account &&
(Features::hasCreateAccountOnFirstLoginFeatures() && $previousUrl === route('filament.admin.auth.login'))
) {
$user = Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->first();

if ($user) {
return $this->alreadyRegistered($user, $account, $provider, $providerAccount);
}

return $this->register($provider, $providerAccount);
}

if (! Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors(
$messageBag
);
}

if (Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) {
if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors(
$messageBag
);
}

$user = $this->createsUser->create($provider, $providerAccount);

return $this->login($user);
}

if (! Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with this :Provider sign in was not found. Please register or try a different sign in method.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors(
$messageBag
);
}

if (Features::hasCreateAccountOnFirstLoginFeatures() && ! $account) {
if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors(
$messageBag
);
}

$user = $this->createsUser->create($provider, $providerAccount);

return $this->login($user);
}

$user = $account->user;

$this->updatesConnectedAccounts->update($user, $account, $provider, $providerAccount);

return $this->login($user);
}

/**
* Handle connection of accounts for an already authenticated user.
*/
protected function alreadyAuthenticated(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse
{
// Connect the account to the user.
if (! $account) {
$this->createsConnectedAccounts->create($user, $provider, $providerAccount);

return redirect()->route('profile')->with(
'status', __('You have successfully connected :Provider to your account.', ['provider' => Providers::name($provider)])
);
}

if ($account->user_id !== $user->id) {
return redirect()->route('profile')->withErrors(
['callback' => __('This :Provider sign in account is already associated with another user. Please log in with that user or connect a different :Provider account.', ['provider' => Providers::name($provider)])]
);
}

// Account already connected
return redirect()->route('profile')->withErrors(
['callback' => __('This :Provider sign in account is already associated with your user.', ['provider' => Providers::name($provider)])]
);
}

/**
* Handle when a user is already registered.
*/
protected function alreadyRegistered(Authenticatable $user, ?ConnectedAccount $account, string $provider, ProviderUser $providerAccount): RedirectResponse
{
if (Features::hasLoginOnRegistrationFeatures()) {
// The user exists, but they're not registered with the given provider.
if (! $account) {
$this->createsConnectedAccounts->create($user, $provider, $providerAccount);
}

return $this->login($user);
}

$messageBag = new MessageBag;
$messageBag->add('socialstream', __('An account with that :Provider sign in already exists, please login.', ['provider' => Providers::name($provider)]));

return redirect()->route('filament.admin.auth.login')->withErrors($messageBag);
}

/**
* Handle the registration of a new user.
*/
protected function register(string $provider, ProviderUser $providerAccount): RedirectResponse
{
if (! $providerAccount->getEmail()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('No email address is associated with this :Provider account. Please try a different account.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors($messageBag);
}

if (Socialstream::newUserModel()->where('email', $providerAccount->getEmail())->exists()) {
$messageBag = new MessageBag;
$messageBag->add(
'socialstream',
__('An account with that email address already exists. Please login to connect your :Provider account.', ['provider' => Providers::name($provider)])
);

return redirect()->route('filament.admin.auth.login')->withErrors($messageBag);
}

$user = $this->createsUser->create($provider, $providerAccount);

return $this->login($user);
}

/**
* Authenticate the given user and return a login response.
*/
protected function login(Authenticatable $user): RedirectResponse
{
$this->guard->login($user, Socialstream::hasRememberSessionFeatures());

return redirect()->route('filament.admin.pages.dashboard');
Expand Down
11 changes: 8 additions & 3 deletions src/SocialstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ public function boot(): void
}

if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/socialstream.php' => config_path('socialstream.php'),
], 'socialstream-config');
match (true) {
$this->hasComposerPackage('filament/filament') => $this->publishes([
__DIR__.'/../config/filament.php' => config_path('socialstream.php'),
], 'socialstream-config'),
default => $this->publishes([
__DIR__.'/../config/socialstream.php' => config_path('socialstream.php'),
], 'socialstream-config')
};
}
}

Expand Down

0 comments on commit 13b5c68

Please sign in to comment.