Skip to content

Commit

Permalink
try to complete oauth sign in method
Browse files Browse the repository at this point in the history
  • Loading branch information
a1383n committed Mar 21, 2024
1 parent b0d51f7 commit b5ce05d
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 82 deletions.
3 changes: 3 additions & 0 deletions src/Contracts/AuthenticatableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use LaravelAuthPro\Contracts\Base\HasBuilderInterface;
use LaravelAuthPro\Enums\AuthIdentifierType;

Expand Down Expand Up @@ -37,4 +38,6 @@ public function getId(): string;
* @return array
*/
public static function getIdentifierMapper(): array;

public function authProviders(): HasMany;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use LaravelAuthPro\Contracts\AuthCredentialInterface;
use LaravelAuthPro\Contracts\Credentials\Base\HasEmailInterface;

interface GoogleCredentialInterface extends AuthCredentialInterface, HasEmailInterface
interface OAuthCredentialInterface extends AuthCredentialInterface, HasEmailInterface
{
public function getIdToken(): string;

public function getDriver(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use LaravelAuthPro\Contracts\AuthenticatableInterface;
use LaravelAuthPro\Contracts\AuthProviderInterface;

interface GoogleProviderInterface extends AuthProviderInterface
interface OAuthProviderInterface extends AuthProviderInterface
{
public function createUserWithGoogleIdToken(string $idToken): AuthenticatableInterface;
public function createUserWithIdToken(string $driver, string $idToken): AuthenticatableInterface;
}
33 changes: 33 additions & 0 deletions src/Credentials/OAuthCredential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace LaravelAuthPro\Credentials;

use LaravelAuthPro\Contracts\Credentials\OAuthCredentialInterface;
use LaravelAuthPro\Enums\AuthIdentifierType;

class OAuthCredential extends AuthCredential implements OAuthCredentialInterface
{
protected ?string $driver;
protected ?string $email;
protected ?string $idToken;

public function getSupportedIdentifiersTypes(): array
{
return [AuthIdentifierType::EMAIL];
}

public function getEmail(): string
{
return $this->email;
}

public function getIdToken(): string
{
return $this->idToken;
}

public function getDriver(): string
{
return $this->driver;
}
}
12 changes: 12 additions & 0 deletions src/Model/UserAuthProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace LaravelAuthPro\Model;

use Illuminate\Database\Eloquent\Model;

class UserAuthProvider extends Model
{
protected $casts = [
'payload' => 'json'
];
}
26 changes: 0 additions & 26 deletions src/Providers/GoogleProvider.php

This file was deleted.

40 changes: 40 additions & 0 deletions src/Providers/OAuthProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace LaravelAuthPro\Providers;

use Laravel\Socialite\Facades\Socialite;
use LaravelAuthPro\Contracts\AuthenticatableInterface;
use LaravelAuthPro\Contracts\Providers\OAuthProviderInterface;
use LaravelAuthPro\Enums\AuthIdentifierType;
use LaravelAuthPro\Enums\AuthProviderSignInMethod;
use LaravelAuthPro\Enums\AuthProviderType;

class OAuthProvider extends AuthProvider implements OAuthProviderInterface
{
public const ID = 'oauth';
public const TYPE = AuthProviderType::OAUTH;
public const IDENTIFIER_TYPE = AuthIdentifierType::EMAIL;
public const SUPPORTED_SIGN_IN_METHODS = [
AuthProviderSignInMethod::OAUTH,
];

public function createUserWithIdToken(string $driver, string $idToken): AuthenticatableInterface
{
$user = Socialite::driver($driver)
->userFromToken($idToken);

$authenticatable = $this->createAuthenticatable($user->getEmail());

$authenticatable->authProviders()
->create([
'provider_type' => static::TYPE,
'provider_id' => static::ID . '.' . $driver,
'payload' => [
'id' => $user->getId(),
'extra' => $user->getRaw()
]
]);

return $authenticatable;
}
}
14 changes: 5 additions & 9 deletions src/SignInMethods/OAuthSignInMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
namespace LaravelAuthPro\SignInMethods;

use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\GoogleProvider;
use LaravelAuthPro\Contracts\AuthCredentialInterface;
use LaravelAuthPro\Contracts\AuthenticatableInterface;
use LaravelAuthPro\Contracts\AuthSignInMethodInterface;
use LaravelAuthPro\Contracts\Credentials\GoogleCredentialInterface;
use LaravelAuthPro\Contracts\Credentials\OAuthCredentialInterface;
use LaravelAuthPro\Contracts\Exceptions\AuthException;

class OAuthSignInMethod implements AuthSignInMethodInterface
{
/**
* @param AuthenticatableInterface $user
* @param GoogleCredentialInterface $credential
* @param OAuthCredentialInterface|AuthCredentialInterface $credential
* @return AuthenticatableInterface
* @throws AuthException
*/
public function __invoke(AuthenticatableInterface $user, GoogleCredentialInterface|AuthCredentialInterface $credential): AuthenticatableInterface
public function __invoke(AuthenticatableInterface $user, OAuthCredentialInterface|AuthCredentialInterface $credential): AuthenticatableInterface
{
try {
/**
* @var GoogleProvider $socialite
*/
$socialite = Socialite::driver($credential->getProviderId());
$oauthUser = Socialite::driver($credential->getDriver())->userFromToken($credential->getIdToken());

$oauthUser = $socialite->userFromToken($credential->getIdToken());
dump($user, $oauthUser);

throw new \Exception('not implemented');
} catch (\Exception $e) {
Expand Down
7 changes: 7 additions & 0 deletions src/Traits/AuthProAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use LaravelAuthPro\Contracts\AuthenticatableInterface;
use LaravelAuthPro\Contracts\AuthIdentifierInterface;
use LaravelAuthPro\Enums\AuthIdentifierType;
use LaravelAuthPro\Model\Builder\AuthenticatableBuilder;
use LaravelAuthPro\Model\UserAuthProvider;

/**
* @mixin AuthenticatableInterface
Expand Down Expand Up @@ -49,4 +51,9 @@ public function getIdentifierMapper(): array
'mobile' => AuthIdentifierType::MOBILE,
];
}

public function authProviders(): HasMany
{
return $this->hasMany(UserAuthProvider::class, 'user_id');
}
}
44 changes: 0 additions & 44 deletions src/Traits/AuthProviderTrait.php

This file was deleted.

0 comments on commit b5ce05d

Please sign in to comment.