Skip to content
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.0",
"leafs/date": "*",
"leafs/password": "*",
"leafs/session": "*",
Expand Down
9 changes: 7 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withDowngradeSets(false, false, false, false, false, true)
->withDowngradeSets(php80: true)
->withFluentCallNewLine()
->withImportNames(true, true, true, true)
->withImportNames(
importNames: true,
importDocBlockNames: true,
importShortClasses: true,
removeUnusedImports: true,
)
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
Expand Down
13 changes: 7 additions & 6 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Leaf;

use League\OAuth2\Client\Provider\AbstractProvider;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
Expand Down Expand Up @@ -43,7 +44,7 @@ class Auth
*/
protected $errorsArray = [];

/** @var array<string, \League\OAuth2\Client\Provider\AbstractProvider> Configured oauth clients */
/** @var array<string, AbstractProvider> Configured oauth clients */
protected $oauthClients = [];

public function __construct()
Expand Down Expand Up @@ -201,7 +202,7 @@ public function withGoogle(
* ---
* Register a generic OAuth client to use with Leaf Auth, should be a league/oauth2-client compatible client.
* @param string $clientName The name of the client to register
* @param \League\OAuth2\Client\Provider\AbstractProvider $client An instance of a league/oauth2-client compatible client
* @param AbstractProvider $client An instance of a league/oauth2-client compatible client
* @return static
*/
public function withProvider(string $clientName, $client)
Expand All @@ -213,7 +214,7 @@ public function withProvider(string $clientName, $client)
/**
* Return an oauth client
* @param string $clientName The name of the client to return
* @return \League\OAuth2\Client\Provider\AbstractProvider|null
* @return AbstractProvider|null
*/
public function client(string $clientName)
{
Expand Down Expand Up @@ -569,7 +570,7 @@ public function find($id)
*
* @param array<string, mixed> $userData The user details to save
* @return User|false|never
* @throws \Exception If database connection is not established
* @throws Exception If database connection is not established
*/
public function createUserFor($userData)
{
Expand Down Expand Up @@ -754,7 +755,7 @@ public function tokens()
* @param string $middleware The middleware to register
* @param callable $callback The callback to run if middleware fails
* @return void|never
* @throws \Exception If not used with leafs/leaf installed
* @throws Exception If not used with leafs/leaf installed
*/
public function middleware(string $middleware, callable $callback)
{
Expand Down Expand Up @@ -958,7 +959,7 @@ protected function getFromSession($value)

/**
* @return void|never
* @throws \Exception If sessions are not enabled
* @throws Exception If sessions are not enabled
*/
protected function sessionCheck()
{
Expand Down
6 changes: 1 addition & 5 deletions src/Auth/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class Model
protected array $dataToSave = [];

/**
* @param array{
* db: \Leaf\Db,
* user: User,
* table: string,
* } $data
* @param array{db: Db, user: User, table: string} $data
*/
public function __construct($data)
{
Expand Down
29 changes: 17 additions & 12 deletions src/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Leaf\Auth;

use Exception;
use Throwable;
use ReturnTypeWillChange;
use Leaf\Db;
use Firebase\JWT\JWT;
use Leaf\Date;
use Leaf\Helpers\Password;
Expand All @@ -22,7 +26,7 @@ class User

/**
* Internal instance of Leaf database
* @var \Leaf\Db
* @var Db
*/
protected $db;

Expand Down Expand Up @@ -82,7 +86,7 @@ public function __construct($data, $session = true)
$sessionLifetime = strtotime($sessionLifetime);

if (!$sessionLifetime) {
throw new \Exception('Invalid session lifetime');
throw new Exception('Invalid session lifetime');
}
} else {
$sessionLifetime = time() + $sessionLifetime;
Expand Down Expand Up @@ -165,8 +169,8 @@ public function update(array $userData): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

if (Config::get('session')) {
Expand Down Expand Up @@ -226,8 +230,8 @@ public function updatePassword(string $oldPassword, string $newPassword): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

$this->data[$passwordKey] = $newPassword;
Expand Down Expand Up @@ -268,8 +272,8 @@ public function resetPassword(string $newPassword): bool
$this->errorsArray = array_merge($this->errorsArray, $this->db->errors());
return false;
}
} catch (\Throwable $th) {
throw new \Exception($th->getMessage());
} catch (Throwable $th) {
throw new Exception($th->getMessage());
}

$this->data[$passwordKey] = $newPassword;
Expand Down Expand Up @@ -390,7 +394,7 @@ public function verifyEmail(): bool
->execute();

return true;
} catch (\Throwable $th) {
} catch (Throwable $th) {
return false;
}
}
Expand Down Expand Up @@ -425,7 +429,7 @@ public function get()

/**
* Set user db instance
* @param \Leaf\Db $db
* @param Db $db
* @return User
*/
public function setDb($db)
Expand All @@ -444,6 +448,7 @@ public function errors()
return $this->errorsArray;
}

#[ReturnTypeWillChange]
public function __toString()
{
return json_encode($this->get()) ?: '';
Expand Down Expand Up @@ -501,14 +506,14 @@ public function __unset($name)
*
* @param mixed $method The table to relate to
* @param mixed $args
* @throws \Exception
* @throws Exception
*
* @return Model
*/
public function __call($method, $args)
{
if (!class_exists('Leaf\App')) {
throw new \Exception('Relations are only available in Leaf apps.');
throw new Exception('Relations are only available in Leaf apps.');
}

return (new Model([
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Leaf\Auth;
use Leaf\Db;
use Leaf\Helpers\Password;

dataset('test-user', [[[
'username' => 'test-user',
Expand Down Expand Up @@ -38,6 +39,7 @@ function authInstance(): Auth
{
$auth = new Auth();
$auth->dbConnection(dbInstance()->connection());
$auth->config('token.secret', Password::hash(uniqid()));

return $auth;
}
Expand Down