Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prelogin Hook and Aborted Login Exceptions #23

Merged
merged 19 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ Then publish the config file with:
php artisan vendor:publish --tag="socialment-config"
```

Finally, edit your panel's `tailwind.config.js` content section to include the following:

```js
content: [
"./app/Filament/**/*.php",
"./resources/views/filament/**/*.blade.php",
"./vendor/filament/**/*.blade.php",

// Ensure the line below is listed
"./vendor/chrisreedio/socialment/resources/**/*.blade.php",
],
```

#### Provider Configuration

> [!IMPORTANT]
Expand Down
16 changes: 16 additions & 0 deletions resources/views/login-error.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="rounded-md bg-red-50/80 p-4">
<div class="flex">
<div class="flex-shrink-0">
{{-- <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
</svg> --}}
<x-icon name="far-circle-x" class='w-8 text-red-400' />
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800">Login Failed</h3>
<div class="mt-2 text-sm text-red-700">
{{ $message }}
</div>
</div>
</div>
</div>
30 changes: 27 additions & 3 deletions src/Controllers/SocialmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace ChrisReedIO\Socialment\Controllers;

use ChrisReedIO\Socialment\Exceptions\AbortedLoginException;
use ChrisReedIO\Socialment\Facades\Socialment;
use ChrisReedIO\Socialment\Models\ConnectedAccount;
use ChrisReedIO\Socialment\SocialmentPlugin;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Session;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\InvalidStateException;

Expand All @@ -23,6 +25,9 @@ public function redirect(string $provider)

public function callback(string $provider)
{
/** @var SocialmentPlugin $plugin */
$plugin = Socialment::getFacadeRoot();

try {
/** @var \SocialiteProviders\Manager\OAuth2\User */
$socialUser = Socialite::driver($provider)->user();
Expand Down Expand Up @@ -60,17 +65,36 @@ public function callback(string $provider)

// Associate the user and save this connected account
$connectedAccount->user()->associate($user)->save();
} else {
// Update the connected account with the latest data
$connectedAccount->update([
'name' => $socialUser->getName(),
'nickname' => $socialUser->getNickname(),
'email' => $socialUser->getEmail(),
'avatar' => $socialUser->getAvatar(),
'token' => $socialUser->token,
'refresh_token' => $socialUser->refreshToken,
'expires_at' => $tokenExpiration,
]);
}

$plugin->executePreLogin($connectedAccount);

auth()->login($connectedAccount->user);

/** @var SocialmentPlugin $plugin */
$plugin = Socialment::getFacadeRoot();
$plugin->executePostLogin($connectedAccount);

// TODO - Move this config paramater to a 'getHomeRoute' method on the plugin
return redirect()->route(config('socialment.routes.home'));
} catch (InvalidStateException $e) {
return redirect()->route(Socialment::getLoginRoute());
return redirect()->route($plugin->getLoginRoute());
} catch (\GuzzleHttp\Exception\ClientException $e) {
// TODO - Log this exception
return redirect()->route($plugin->getLoginRoute());
} catch (AbortedLoginException $e) {
Session::flash('socialment.error', $e->getMessage());

return redirect()->route($plugin->getLoginRoute());
}
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/AbortedLoginException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ChrisReedIO\Socialment\Exceptions;

use Exception;

class AbortedLoginException extends Exception
{
public function __construct($message = 'Login aborted', $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
7 changes: 7 additions & 0 deletions src/Models/ConnectedAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
/**
* @property string $provider
* @property string $provider_user_id
* @property string $name
* @property string $nickname
* @property string $email
* @property string $avatar
* @property string $token
* @property string $refresh_token
* @property Datetime $expires_at
*/
class ConnectedAccount extends Model
{
Expand Down
47 changes: 44 additions & 3 deletions src/SocialmentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Concerns\EvaluatesClosures;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;

class SocialmentPlugin implements Plugin
Expand All @@ -15,7 +16,9 @@ class SocialmentPlugin implements Plugin

public bool | Closure | null $visible = null;

public ?Closure $loginCallback = null;
public ?Closure $preLoginCallback = null;

public ?Closure $postLoginCallback = null;

protected string $loginRoute = 'filament.admin.auth.login';

Expand All @@ -26,6 +29,21 @@ public function getId(): string

public function register(Panel $panel): void
{
$panel->renderHook('panels::auth.login.form.before', function () {
$errorMessage = Session::get('socialment.error');

if (! $this->evaluate($this->visible) || ! $errorMessage) {
return '';
}

return View::make(
config('socialment.view.login-error', 'socialment::login-error'),
[
'message' => $errorMessage,
]
);
});

$panel->renderHook('panels::auth.login.form.after', function () {
if (! $this->evaluate($this->visible)) {
return '';
Expand Down Expand Up @@ -88,13 +106,36 @@ public function getLoginRoute(): string
return (string) $this->evaluate($this->loginRoute);
}

/**
* Sets up a callback to be called before a user is logged in.
* This is useful if you wish to check a user's roles before allowing them to login.
* Throw a Socialment\Exceptions\AbortedLoginException to abort the login.
*/
public function preLogin(Closure $callback): static
{
// config()->set('socialment.post_login', $callback);
$this->postLoginCallback = $callback;

return $this;
}

/**
* Executes the pre login callback. Set up closure to execute via the preLogin method.
*/
public function executePreLogin(ConnectedAccount $account): void
{
if ($callback = $this->postLoginCallback) {
($callback)($account);
}
}

/**
* Sets up a callback to be called after a user logs in.
*/
public function postLogin(Closure $callback): static
{
// config()->set('socialment.post_login', $callback);
$this->loginCallback = $callback;
$this->postLoginCallback = $callback;

return $this;
}
Expand All @@ -104,7 +145,7 @@ public function postLogin(Closure $callback): static
*/
public function executePostLogin(ConnectedAccount $account): void
{
if ($callback = $this->loginCallback) {
if ($callback = $this->postLoginCallback) {
($callback)($account);
}
}
Expand Down