Skip to content

Commit

Permalink
chore: magic link arg classes now properly take all required params (#92
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ctran88 authored Dec 11, 2024
1 parent 7afac85 commit c72d387
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
36 changes: 16 additions & 20 deletions custom/lib/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Auth
{
private CachedKeySet $jwks;
private readonly MagicLinksApi $magicLinksApi;

/**
* Auth class that provides methods for validating JWTs and creating Magic Links.
Expand All @@ -40,6 +41,8 @@ public function __construct(private string $appId, private Configuration $config
60 * 60 * 24, // expires in 24 hours
true
);

$this->magicLinksApi = new MagicLinksApi(null, $this->config);
}

/**
Expand Down Expand Up @@ -84,36 +87,31 @@ public function validateJwt(string $jwt): string
*/
public function createMagicLink(
MagicLinkWithEmailArgs|MagicLinkWithPhoneArgs|MagicLinkWithUserArgs $args,
MagicLinkOptions|null $options,
MagicLinkOptions|null $options = null,
): MagicLink {
$identifier = null;
$channel = null;
$payloadData = [
'type' => $args->type,
'send' => $args->send,
];

switch ($args) {
case $args instanceof MagicLinkWithEmailArgs:
$identifier = $args->email;
$channel = MagicLinkChannel::EMAIL;
$payloadData['email'] = $args->email;
$payloadData['channel'] = MagicLinkChannel::EMAIL;
break;
case $args instanceof MagicLinkWithPhoneArgs:
$identifier = $args->phone;
$channel = MagicLinkChannel::PHONE;
$payloadData['phone'] = $args->phone;
$payloadData['channel'] = MagicLinkChannel::PHONE;
break;
case $args instanceof MagicLinkWithUserArgs:
$identifier = $args->userId;
$channel = $args->channel;
$payloadData['user_id'] = $args->userId;
$payloadData['channel'] = $args->channel;
break;
default:
throw new InvalidArgumentException("args must contain an email, phone, or userId");
}

$payload = new CreateMagicLinkRequest(
[
'identifier' => $identifier,
'channel' => $channel,
'type' => $args->type,
'send' => $args->send,
]
);
$payload = new CreateMagicLinkRequest($payloadData);

if ($options) {
if ($options->language) {
Expand All @@ -130,10 +128,8 @@ public function createMagicLink(
}
}

$magicLinksApi = new MagicLinksApi(null, $this->config);

try {
return $magicLinksApi->createMagicLink($this->appId, $payload)->getMagicLink();
return $this->magicLinksApi->createMagicLink($this->appId, $payload)->getMagicLink();
} catch (ApiException $e) {
throw PassageError::fromApiException($e);
}
Expand Down
4 changes: 1 addition & 3 deletions custom/lib/MagicLinkArgsBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Passage\Client;

use OpenAPI\Client\Model\MagicLinkType;

readonly class MagicLinkArgsBase
{
public function __construct(
public readonly MagicLinkType $type,
public readonly string $type,
public readonly bool $send,
) {
}
Expand Down
9 changes: 9 additions & 0 deletions custom/lib/MagicLinkWithEmailArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

final readonly class MagicLinkWithEmailArgs extends MagicLinkArgsBase
{
/**
* @param string $email The email to send the magic link to
* @param string $type The type of magic link to send, should be OpenAPI\Client\Model\MagicLinkType
* @param bool $send Whether to send the magic link
* @see OpenAPI\Client\Model\MagicLinkType
*/
public function __construct(
public readonly string $email,
string $type,
bool $send,
) {
parent::__construct($type, $send);
}
}
9 changes: 9 additions & 0 deletions custom/lib/MagicLinkWithPhoneArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@

final readonly class MagicLinkWithPhoneArgs extends MagicLinkArgsBase
{
/**
* @param string $phone The phone number to send the magic link to
* @param string $type The type of magic link to send, should be OpenAPI\Client\Model\MagicLinkType
* @param bool $send Whether to send the magic link
* @see OpenAPI\Client\Model\MagicLinkType
*/
public function __construct(
public readonly string $phone,
string $type,
bool $send,
) {
parent::__construct($type, $send);
}
}
15 changes: 12 additions & 3 deletions custom/lib/MagicLinkWithUserArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

namespace Passage\Client;

use OpenAPI\Client\Model\MagicLinkChannel;

final readonly class MagicLinkWithUserArgs extends MagicLinkArgsBase
{
/**
* @param string $userId The Passage user ID
* @param string $channel The channel to send the magic link to, should be OpenAPI\Client\Model\MagicLinkChannel
* @param string $type The type of magic link to send, should be OpenAPI\Client\Model\MagicLinkType
* @param bool $send Whether to send the magic link
* @see OpenAPI\Client\Model\MagicLinkChannel
* @see OpenAPI\Client\Model\MagicLinkType
*/
public function __construct(
public readonly string $userId,
public readonly MagicLinkChannel $channel,
public readonly string $channel,
string $type,
bool $send,
) {
parent::__construct($type, $send);
}
}
4 changes: 4 additions & 0 deletions custom/test/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Passage\Test;

use Dotenv\Dotenv;
use OpenAPI\Client\Model\MagicLinkType;
use Passage\Client\MagicLinkWithEmailArgs;
use Passage\Client\MagicLinkWithPhoneArgs;
use Passage\Client\MagicLinkWithUserArgs;
use UnexpectedValueException;
use PHPUnit\Framework\TestCase;
use Passage\Client\Passage;
Expand Down

0 comments on commit c72d387

Please sign in to comment.