-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c34e05
commit 2e09348
Showing
1 changed file
with
142 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,175 @@ | ||
# Axm Socialite | ||
|
||
<!-- markdownlint-disable no-inline-html --> | ||
<p align="center"> | ||
<a href="https://packagist.org/packages/axm/Socialite" | ||
><img | ||
src="https://poser.pugx.org/axm/Socialite/v/stable" | ||
alt="Latest Stable Version" | ||
/></a> | ||
<a href="https://packagist.org/packages/axm/Socialite" | ||
><img | ||
src="https://poser.pugx.org/axm/Socialite/downloads" | ||
alt="Total Downloads" | ||
/></a> | ||
<a href="https://packagist.org/packages/axm/Socialite" | ||
><img | ||
src="https://poser.pugx.org/axm/Socialite/license" | ||
alt="License" | ||
/></a> | ||
<a href="https://packagist.org/packages/axm/Socialite"> | ||
<img src="https://poser.pugx.org/axm/Socialite/v/stable" alt="Latest Stable Version"/></a> | ||
<a href="https://packagist.org/packages/axm/Socialite"> | ||
<img src="https://poser.pugx.org/axm/Socialite/downloads" alt="Total Downloads"/></a> | ||
<a href="https://packagist.org/packages/axm/Socialite"> | ||
<img src="https://poser.pugx.org/axm/Socialite/license" alt="License"/></a> | ||
</p> | ||
<br /> | ||
<br /> | ||
|
||
|
||
## 📦 Installation | ||
|
||
To install the component, add the following to your `composer.json` file: | ||
|
||
```json | ||
{ | ||
"require": { | ||
"axm/socialite": "^1.0" | ||
} | ||
} | ||
``` | ||
|
||
You can also use [Composer](https://getcomposer.org/) to install Axm in your project quickly. | ||
|
||
```bash | ||
composer require axm/Socialite | ||
``` | ||
|
||
## Socialite PHP Library | ||
|
||
Socialite is a PHP library that provides Socialite localization support. It includes an interface `SocialiteInterface` defining the methods required for a Socialite translator and a class `Socialite` implementing this interface for handling Socialite localization. | ||
|
||
### SocialiteInterface | ||
|
||
`getLocale(): string` | ||
|
||
Returns the current locale. | ||
|
||
`trans(string $key, array $params = []): string` | ||
|
||
Translates a key with optional parameters. | ||
Then, run `composer update` to install the package. | ||
|
||
- `$key`: The translation key. | ||
- `$params`: Optional parameters for string interpolation. | ||
|
||
Returns the translated message. | ||
## Configuration | ||
|
||
### Socialite Class | ||
Once the package is installed, you need to configure it. Open the `.env` file and add the following lines: | ||
|
||
### Singleton Pattern | ||
|
||
The `Socialite` class follows the singleton pattern, ensuring only one instance is created throughout the application. | ||
|
||
### Methods | ||
`make(): SocialiteInterface` | ||
|
||
Static method to get an instance of the `Socialite` class. | ||
|
||
`setLocale(): void` | ||
|
||
Sets the current locale and reloads translations. | ||
```php | ||
GOOGLE_CLIENT_ID="YOUR_GOOGLE_CLIENT_ID" | ||
GOOGLE_CLIENT_SECRET="YOUR_GOOGLE_CLIENT_SECRET" | ||
GOOGLE_REDIRECT="${APP_URL}/YOUR_GOOGLE_REDIRECT_URI" | ||
|
||
`getLocale(): string` | ||
FACEBOOK_CLIENT_ID="YOUR_FACEBOOK_CLIENT_ID" | ||
FACEBOOK_CLIENT_SECRET="YOUR_FACEBOOK_CLIENT_SECRET" | ||
FACEBOOK_REDIRECT="${APP_URL}/YOUR_FACEBOOK_REDIRECT_URI" | ||
|
||
Gets the current locale. | ||
``` | ||
|
||
`trans(string $key, array $params = []): string` | ||
Be sure to replace the placeholders with your actual Google and Facebook client IDs, client secrets, and redirect URIs. | ||
|
||
Translates a key with optional parameters. | ||
## Usage | ||
|
||
- `$key`: The translation key. | ||
- `$params`: Optional parameters for string interpolation. | ||
To use the component, simply add the following line to your route file: | ||
|
||
Returns the translated message. | ||
```php | ||
Route::get('/auth-redirect/{provider:\w+}', [App\Raxm\AuthComponent::class, 'handlerAuthRedirect']); | ||
Route::get('/auth-google-callback', [App\Raxm\AuthComponent::class, 'handlerRediretGoogleAuth']); | ||
|
||
`loadTranslationsFromFile(): void` | ||
``` | ||
|
||
Loads translations from Socialite files. This method throws an `AxmException` if an error occurs while loading Socialite files. | ||
This route will redirect the user to the authentication page of the specified provider. | ||
|
||
### Configuration | ||
Once the user has authenticated, they will be redirected back to your application. The `handleSocialAuthRedirect` method will then be called. This method will check if the user is already logged in. If they are, they will be redirected to the home page. If they are not, they will be registered as a new user and then redirected to the home page. | ||
|
||
- `DEFAULT_SocialiteUAGE`: The default Socialite if no locale is set. | ||
## Example | ||
|
||
### Usage | ||
The following example shows how to use the component to authenticate users with Google and FaceBook, | ||
usage in your controller: | ||
|
||
```php | ||
// Get an instance of Socialite | ||
$Socialite = Socialite::make(); | ||
|
||
$Socialite->trans('file.message'); | ||
|
||
//or | ||
<?php | ||
|
||
namespace App\Raxm; | ||
|
||
use App\Models\User; | ||
use Axm\Raxm\Component; | ||
use Axm\Views\View; | ||
use Axm\Socialite\Socialite; | ||
|
||
|
||
class AuthComponent extends Component | ||
{ | ||
/** | ||
* @var array Associative array containing different types of authentication | ||
*/ | ||
protected $typeOfAuth = [ | ||
'google' => 'google', | ||
'facebook' => 'facebook' | ||
]; | ||
|
||
/** | ||
* Redirect to the corresponding social media authentication page | ||
* @return RedirectResponse | ||
*/ | ||
public function handlerAuthRedirect() | ||
{ | ||
$provider = $this->getProvider(); | ||
return Socialite::driver($provider)->redirect(); | ||
} | ||
|
||
/** | ||
* Handle social media authentication and redirect | ||
* | ||
* @param string $driver | ||
* @return void | ||
*/ | ||
protected function handleSocialAuthRedirect($user) | ||
{ | ||
if (app()->login(['email', $user->email])) { | ||
redirect('/home'); | ||
} | ||
} | ||
|
||
/** | ||
* Handle redirect after Google authentication. | ||
* @return void | ||
*/ | ||
public function handlerRediretGoogleAuth() | ||
{ | ||
$user = Socialite::driver('google')->user(); | ||
$this->handleSocialAuthRedirect($user); | ||
|
||
// Register a new user and redirect to home page | ||
$this->registerNewUserAndRedirect($user); | ||
} | ||
|
||
/** | ||
* Handle redirect after Facebook authentication. | ||
* @return void | ||
*/ | ||
public function handlerRediretFacebookAuth() | ||
{ | ||
$user = Socialite::driver('facebook')->user(); | ||
$this->handleSocialAuthRedirect($user); | ||
|
||
// Register a new user and redirect to home page | ||
$this->registerNewUserAndRedirect($user); | ||
} | ||
|
||
/** | ||
* Register new user and redirect | ||
* | ||
* @param object $user | ||
* @return void | ||
*/ | ||
protected function registerNewUserAndRedirect(object $user) | ||
{ | ||
$newUser = new User; | ||
$newUser->name = $user->name; | ||
$newUser->email = $user->email; | ||
$newUser->save(); | ||
|
||
app()->login($newUser); | ||
redirect('/home'); | ||
} | ||
|
||
/** | ||
* This function retrieves the authentication provider from the route parameters | ||
* @return string | ||
*/ | ||
protected function getProvider(): string | ||
{ | ||
$provider = app()->request->getRouteParam('provider'); | ||
if (!array_key_exists($provider, $this->typeOfAuth)) { | ||
throw new \Exception('Authentication provider unknown'); | ||
} | ||
|
||
return $this->typeOfAuth[$provider]; | ||
} | ||
|
||
} | ||
|
||
// Translate a key | ||
$Socialite->trans('file.message', ['param1', 'param2']); | ||
``` |