Skip to content

Commit

Permalink
Use bind on contract, add facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Daynnnnn committed Aug 4, 2021
1 parent be736cf commit 08dcc2d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/Facades/AuthService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Daynnnnn\Statamic\Auth\ForwardAuth\Facades;

use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\AuthServiceContract;
use Illuminate\Support\Facades\Facade;

class AuthService extends Facade
{
protected static function getFacadeAccessor()
{
return AuthServiceContract::class;
}
}
11 changes: 9 additions & 2 deletions src/ForwardAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@

class ForwardAuthServiceProvider extends AddonServiceProvider
{
public function register()
{
$this->app->bind(AuthServices\AuthServiceContract::class, function () {
$class = $this->lookupType(config('auth.providers.users.type'));
return new $class;
});
}

public function boot()
{
Auth::provider('forward', function () {
$class = $this->lookupType(config('auth.providers.users.type'));
return new ForwardAuthUserProvider(new $class);
return new ForwardAuthUserProvider;
});
}

Expand Down
18 changes: 6 additions & 12 deletions src/ForwardAuthUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Daynnnnn\Statamic\Auth\ForwardAuth;

use Daynnnnn\Statamic\Auth\ForwardAuth\AuthServices\AuthServiceContract;
use Daynnnnn\Statamic\Auth\ForwardAuth\Facades\AuthService;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderContract;
use Illuminate\Support\Facades\Hash;
Expand All @@ -11,22 +11,16 @@

class ForwardAuthUserProvider extends UserProvider implements UserProviderContract
{
protected $authService;

public function __construct(AuthServiceContract $authService) {
$this->authService = $authService;
}

public function retrieveByCredentials(array $credentials)
{
if (($user = User::findByEmail($credentials['email'])) === null) {
$this->authService->checkCredentialsAgainstForwardAuth($credentials);
AuthService::checkCredentialsAgainstForwardAuth($credentials);

if ($this->authService->credentialsValidAgainstForwardAuth()) {
if (AuthService::credentialsValidAgainstForwardAuth()) {
return User::make()
->email($credentials['email'])
->password($credentials['password'])
->data($this->authService->userData())
->data(AuthService::userData())
->save();
}
}
Expand All @@ -39,9 +33,9 @@ public function validateCredentials(Authenticatable $user, array $credentials)
$localCredentialsValid = Hash::check($credentials['password'], $user->getAuthPassword());

if ($user->forward_auth === true) {
$this->authService->checkCredentialsAgainstForwardAuth($credentials);
AuthService::checkCredentialsAgainstForwardAuth($credentials);

if (!$this->authService->credentialsValidAgainstForwardAuth()) {
if (!AuthService::credentialsValidAgainstForwardAuth()) {
$localCredentialsValid = false;
} elseif (!$localCredentialsValid) {
$user->password($credentials['password'])->save();
Expand Down

0 comments on commit 08dcc2d

Please sign in to comment.