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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
"phpstan/phpstan": "^2.1",
"phpstan/extension-installer": "^1.4",
"leafs/leaf": "^4.4",
"league/oauth2-google": "^4.0",
"leafs/billing": "^0.2.0",
"rector/rector": "^2.2"
"rector/rector": "^2.2",
"league/oauth2-google": "^5.0"
},
"scripts": {
"alchemy": "./vendor/bin/alchemy setup",
Expand Down
33 changes: 23 additions & 10 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,35 @@ public function __construct()
});
}

if (
class_exists('League\OAuth2\Client\Provider\Google') &&
_env('GOOGLE_AUTH_CLIENT_ID') &&
_env('GOOGLE_AUTH_CLIENT_SECRET')
) {
if ($this->env('GOOGLE_AUTH_CLIENT_ID') && $this->env('GOOGLE_AUTH_CLIENT_SECRET') && class_exists(Google::class)) {
$this->withGoogle(
_env('GOOGLE_AUTH_CLIENT_ID'),
_env('GOOGLE_AUTH_CLIENT_SECRET'),
$this->env('GOOGLE_AUTH_CLIENT_ID'),
$this->env('GOOGLE_AUTH_CLIENT_SECRET'),
[
Comment on lines +101 to 105
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new env-based auto-withGoogle() path in __construct() isn’t covered by tests. Consider adding a small Pest test that sets values via putenv()/$_SERVER (without Leaf’s _env() present) and asserts the Google client is registered when league/oauth2-google is installed.

Copilot uses AI. Check for mistakes.
'name' => 'google',
'redirectUri' => _env('GOOGLE_AUTH_REDIRECT_URI', _env('APP_URL') . '/auth/register/google'),
'redirectUri' => $this->env(
'GOOGLE_AUTH_REDIRECT_URI',
$this->env('APP_URL') . '/auth/register/google'
) ?: '',
]
);
}
}

/** @return mixed Returns the value of the environment variable by using Leaf's `_env` primarily */
private function env(string $name, $default = false)
{
// If `_env` function of Leaf is defined, use it.
if (function_exists('_env')) {
return _env($name, $default);
}

// Return the value if found, otherwise false like getenv().
return $_ENV[$name] ?? $default;
Comment on lines +124 to +125
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-Leaf fallback reads only from $_ENV. Environment variables are often exposed via getenv()/$_SERVER without populating $_ENV, so this can return the default even when the variable is set. Consider falling back to $_SERVER[$name] and/or getenv($name) before returning $default.

Suggested change
// Return the value if found, otherwise false like getenv().
return $_ENV[$name] ?? $default;
// Prefer $_ENV, then $_SERVER, then getenv(), finally falling back to the provided default.
if (isset($_ENV[$name])) {
return $_ENV[$name];
}
if (isset($_SERVER[$name])) {
return $_SERVER[$name];
}
$value = getenv($name);
if ($value !== false) {
return $value;
}
return $default;

Copilot uses AI. Check for mistakes.
}

/**

/**
Comment on lines +129 to 130
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s an extra /** starting at line 128 (and another /** on line 130) which makes this docblock malformed and harder for tooling (PHPStan/PHPDoc generators) to parse. Remove the stray opener so the connect() docblock starts cleanly with a single /**.

Suggested change
/**

Copilot uses AI. Check for mistakes.
* Connect leaf auth to the database
* @param array{
Expand Down Expand Up @@ -171,7 +184,7 @@ public function dbConnection(PDO $connection)
* @param array{
* name?: string,
* redirectUri?: string,
* } $options
* } $options If `$options['redirectUri']` is not set, it will default to `$_ENV['APP_URL']/auth/google/callback`
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPDoc for withGoogle() says the default redirect URI is based on $_ENV['APP_URL'], but the implementation now uses $this->env('APP_URL') (which may resolve via Leaf _env() or other sources). Update the docstring to match the actual behavior (and clarify behavior when APP_URL is unset).

Suggested change
* } $options If `$options['redirectUri']` is not set, it will default to `$_ENV['APP_URL']/auth/google/callback`
* } $options If `$options['redirectUri']` is not set, it will default to the value of `APP_URL` (as resolved by this class's environment helper) with `/auth/google/callback` appended; if `APP_URL` is unset or empty, the default will be `/auth/google/callback`.

Copilot uses AI. Check for mistakes.
* @return static
*/
public function withGoogle(
Expand All @@ -184,7 +197,7 @@ public function withGoogle(
unset($options['name']);

if (!isset($options['redirectUri'])) {
$options['redirectUri'] = _env('APP_URL') . '/auth/google/callback';
$options['redirectUri'] = $this->env('APP_URL') . '/auth/google/callback';
}

$this->withProvider($clientName, new Google(array_merge([
Expand Down
Loading