Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbutcher committed Jan 5, 2024
1 parent 5fca3f2 commit 292c068
Show file tree
Hide file tree
Showing 30 changed files with 760 additions and 142 deletions.
10 changes: 10 additions & 0 deletions config/socialstream.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Providers\RouteServiceProvider;
use JoelButcher\Socialstream\Features;
use JoelButcher\Socialstream\Providers;

Expand All @@ -16,4 +17,13 @@
Features::providerAvatars(),
Features::refreshOAuthTokens(),
],
'home' => RouteServiceProvider::HOME,
'redirects' => [
'login' => RouteServiceProvider::HOME,
'register' => RouteServiceProvider::HOME,
'login-failed' => '/login',
'registration-failed' => '/register',
'provider-linked' => '/user/profile',
'provider-link-failed' => '/user/profile',
]
];
279 changes: 158 additions & 121 deletions src/Actions/AuthenticateOAuthCallback.php

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/Contracts/AuthenticatesOAuthCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace JoelButcher\Socialstream\Contracts;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Laravel\Fortify\Contracts\LoginResponse;
use Laravel\Socialite\Contracts\User;

interface AuthenticatesOAuthCallback
{
/**
* Authenticates users returning from an OAuth flow.
*/
public function authenticate(string $provider, User $providerAccount): Response|RedirectResponse|LoginResponse;
public function authenticate(string $provider, User $providerAccount): SocialstreamResponse|RedirectResponse;
}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthLoginFailedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthLoginFailedResponse extends SocialstreamResponse
{

}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthLoginResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthLoginResponse extends SocialstreamResponse
{

}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthProviderLinkFailedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthProviderLinkFailedResponse extends SocialstreamResponse
{

}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthProviderLinkedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthProviderLinkedResponse extends SocialstreamResponse
{

}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthRegisterFailedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthRegisterFailedResponse extends SocialstreamResponse
{

}
8 changes: 8 additions & 0 deletions src/Contracts/OAuthRegisterResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

interface OAuthRegisterResponse extends SocialstreamResponse
{

}
10 changes: 10 additions & 0 deletions src/Contracts/SocialstreamResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace JoelButcher\Socialstream\Contracts;

use Illuminate\Contracts\Support\Responsable;

interface SocialstreamResponse extends Responsable
{

}
4 changes: 1 addition & 3 deletions src/Events/ConnectedAccountEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ abstract class ConnectedAccountEvent

/**
* Create a new event instance.
*
* @param ConnectedAccount $connectedAccount
*/
public function __construct(public $connectedAccount)
public function __construct(public mixed $connectedAccount)
{
//
}
Expand Down
24 changes: 24 additions & 0 deletions src/Events/NewOAuthRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class NewOAuthRegistration
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public mixed $user,
public string $provider,
public ProviderUser $providerAccount,
) {
//
}
}
25 changes: 25 additions & 0 deletions src/Events/OAuthLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class OAuthLogin
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public mixed $user,
public string $provider,
public mixed $connectedAccount,
public ProviderUser $providerAccount,
) {
//
}
}
23 changes: 23 additions & 0 deletions src/Events/OAuthLoginFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class OAuthLoginFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public string $provider,
public ProviderUser $providerAccount,
) {
//
}
}
25 changes: 25 additions & 0 deletions src/Events/OAuthProviderLinkFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class OAuthProviderLinkFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public mixed $user,
public string $provider,
public mixed $connectedAccount,
public ProviderUser $providerAccount,
) {
//
}
}
25 changes: 25 additions & 0 deletions src/Events/OAuthProviderLinked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class OAuthProviderLinked
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public mixed $user,
public string $provider,
public mixed $connectedAccount,
public ProviderUser $providerAccount,
) {
//
}
}
24 changes: 24 additions & 0 deletions src/Events/OAuthRegistrationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JoelButcher\Socialstream\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Laravel\Socialite\Contracts\User as ProviderUser;

class OAuthRegistrationFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public string $provider,
public mixed $connectedAccount,
public ProviderUser $providerAccount,
) {
//
}
}
16 changes: 15 additions & 1 deletion src/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ public static function hasCreateAccountOnFirstLoginFeatures(): bool
return static::enabled(static::createAccountOnFirstLogin());
}

/**
* Determine if the application supports social login
* authentication from pages other than "/login".
*/
public static function hasGlobalLoginFeatures(): bool
{
return static::enabled(static::globalLogin());
}

/**
* Determine if the application supports logging into existing
* accounts when registering with a provider who's email address
* accounts when registering with a provider whose email address
* is already registered.
*/
public static function hasLoginOnRegistrationFeatures(): bool
Expand Down Expand Up @@ -79,6 +88,11 @@ public static function createAccountOnFirstLogin(): string
return 'create-account-on-first-login';
}

public static function globalLogin(): string
{
return 'global-login';
}

/**
* Enable the login on registration feature.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use JoelButcher\Socialstream\Contracts\HandlesInvalidState;
use JoelButcher\Socialstream\Contracts\HandlesOAuthCallbackErrors;
use JoelButcher\Socialstream\Contracts\ResolvesSocialiteUsers;
use Laravel\Fortify\Contracts\LoginResponse;
use JoelButcher\Socialstream\Contracts\SocialstreamResponse;
use Laravel\Socialite\Two\InvalidStateException;
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse;

Expand Down Expand Up @@ -42,7 +42,7 @@ public function redirect(string $provider, GeneratesProviderRedirect $generator)
/**
* Attempt to log the user in via the provider user returned from Socialite.
*/
public function callback(Request $request, string $provider): Response|RedirectResponse|LoginResponse
public function callback(Request $request, string $provider): SocialstreamResponse|RedirectResponse|Response
{
$redirect = $this->errorHandler->handle($request);

Expand Down
39 changes: 39 additions & 0 deletions src/Http/Responses/OAuthLoginResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace JoelButcher\Socialstream\Http\Responses;

use App\Providers\RouteServiceProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route;
use JoelButcher\Socialstream\Concerns\InteractsWithComposer;
use JoelButcher\Socialstream\Contracts\OAuthLoginResponse as LoginResponseContract;
use JoelButcher\Socialstream\Socialstream;
use Laravel\Fortify\Contracts\LoginResponse as FortifyLoginResponse;

class OAuthLoginResponse implements LoginResponseContract
{
use InteractsWithComposer;

public function toResponse($request): RedirectResponse
{
return Socialstream::redirects('login')
? redirect()->intended(Socialstream::redirects('login'))
: $this->defaultResponse();
}

private function defaultResponse(): RedirectResponse|FortifyLoginResponse
{
$previousUrl = session()->pull('socialstream.previous_url');

return match (true) {
Route::has('filament.auth.login') && $previousUrl === route('filament.auth.login') => redirect()
->route('admin'),
$this->hasComposerPackage('laravel/breeze') => redirect()
->route('dashboard'),
$this->hasComposerPackage('laravel/jetstream') => app(FortifyLoginResponse::class),
default => redirect()
->to(RouteServiceProvider::HOME),
};
}

}
33 changes: 33 additions & 0 deletions src/Http/Responses/OAuthOAuthLoginFailedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace JoelButcher\Socialstream\Http\Responses;

use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Route;
use JoelButcher\Socialstream\Concerns\InteractsWithComposer;
use JoelButcher\Socialstream\Contracts\OAuthLoginFailedResponse as OAuthLoginFailedResponseContract;
use JoelButcher\Socialstream\Socialstream;

class OAuthOAuthLoginFailedResponse implements OAuthLoginFailedResponseContract
{
use InteractsWithComposer;

public function toResponse($request): RedirectResponse
{
return Socialstream::redirects('login-failed')
? redirect()->intended(Socialstream::redirects('login-failed'))
: $this->defaultResponse();
}

private function defaultResponse(): RedirectResponse
{
$previousUrl = session()->pull('socialstream.previous_url');

return redirect()->to(match (true) {
Route::has('filament.auth.login') && $previousUrl === route('filament.auth.login') => 'filament.auth.login',
Route::has('login') && $previousUrl === route('login') => 'login',
Route::has('register') && $previousUrl === route('register') => 'register',
default => 'login',
});
}
}
Loading

0 comments on commit 292c068

Please sign in to comment.