This package only handles the authentication layer, securely saving, retrieving and revoking tokens around google api client. So it could be a good choice as authentication layer while using any api service from google.
For some common api services from google please check:
Gmail Api: skn036/laravel-gmail-api
Calendar Api: skn036/laravel-google-calendar (coming soon)
For extending your own api service please follow the guidelines below.
First install the package via composer.
composer require skn036/laravel-google-client
Then publish the config file. It will publishes google.php file in the config directory.
php artisan vendor:publish --provider="Skn036\Google\GoogleClientServiceProvider"
Please look into config/google.php for detailed information about the configuration. First you need to enable the required api services and generate credentials in your google developers account. Please see here from detailed instructions. Available .env variables for configuration are:
GOOGLE_APPLICATION_NAME=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=
GOOGLE_SCOPES=
GOOGLE_PUB_SUB_TOPIC=
There are three authentication modes:
- Single account in whole application ( set
credentials_per_user
tofalse
in config file ). - Single account per user ( set
credentials_per_user
totrue
&multiple_accounts_per_user
tofalse
in config file ). - Multiple accounts per user ( set
credentials_per_user
totrue
&multiple_accounts_per_user
totrue
in config file ).
Note: You need to set the scopes on .env file which google services you intended to use.
use Illuminate\Http\Request;
use Skn036\Google\GoogleClient;
// pass the google client to the view
Route::get('/google-dashboard', function (Request $request) {
$googleClient = new GoogleClient();
return view('google-dashboard', compact('googleClient'));
})->name('google-dashboard');
// redirect go google oauth2 login page
Route::post('/google-login', function (Request $request) {
return (new GoogleClient())->redirectToAuthUrl();
})->name('google-login');
// logout from google account
Route::post('/google-logout', function (Request $request) {
(new GoogleClient())->logout();
return redirect()->route('google-dashboard');
})->name('google-logout');
// authenticate the account from google oauth2 login success
// this route must match the redirect uri in google console and .env file
Route::get('/google-auth-callback', function (Request $request) {
(new GoogleClient())->authenticate();
return redirect()->route('google-dashboard');
});
google-dashboard.blade.php:
<!-- login or logout -->
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8 flex flex-row items-center gap-4">
@if ($googleClient->isAuthenticated())
<form method="POST" action="{{ route('google-logout') }}">
@csrf
<x-primary-button> {{ __("Logout") }} </x-primary-button>
</form>
@else
<form method="POST" action="{{ route('google-login') }}">
@csrf
<x-primary-button> {{ __("Login with Google") }} </x-primary-button>
</form>
@endif
</div>
<!-- status -->
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8 space-y-6">
{{ $googleClient->isAuthenticated() ? "Authenticated user:" . $googleClient->email : 'Not
authenticated' }}
</div>
If you are using SPA frontend like React
or Vue
and entirely handle the Oauth redirect in the frontend, you should call the getAuthUrl method to get the google Oauth url.
$url = $googleClient->getAuthUrl();
return response()->json($url);
After successful login from google, it should add code
route query param on redirect uri set on the google console. You need to pass this code the authenticate
method or if you pass the code as route query, it will automatically capture on authentication request;
$token = $googleClient->authenticate($request->code);
By default, authenticated user is passed to GoogleClient
instance using laravel's auth()->id()
. If you want to pass a user manually to client instance:
$googleClient = new GoogleClient(1); // pass the user's id
When using multiple accounts, first account synced will be set as default account. If you want to change the default account, first get the available accounts:
$accounts = $googleClient->getSyncedAccounts();
It will give a array of synced accounts like:
[
['email' => 'john@gmail.com', 'profile' => ['givenName' => 'John', 'familyName' => 'Doe', ...] ],
['email' => 'foo@gmail.com', 'profile' => ['givenName' => 'Foo', 'familyName' => 'Bar', ...] ],
];
Then you call setDefaultAccount
method to change the default account.
$googleClient = $googleClient->setDefaultAccount('foo@gmail.com');
Normally it will use the default account to interact with the services. If you want to use other account rather than the default, you should pass the email of the intended account as the second argument of the client constructor.
$googleClient = new GoogleClient(1, 'foo@gmail.com');
First you should make a api service class extending the GoogleClient class. Then bind it to the api services to interact with the api.
For example we will extend to the Gmail api
service:
use Skn036\Google\GoogleClient;
use YourNamespace\Gmail\GmailMessage;
class Gmail extends GoogleClient
{
public function __construct(
string|int|null $userId = null,
?string $usingAccount = null,
?array $config = null
) {
parent::__construct($userId, $usingAccount, $config);
}
// access the messages on gmail api
public function messages()
{
return new GmailMessage($this);
}
}
On the GmailMessage.php
:
namespace YourNamespace\Gmail;
class GmailMessage
{
protected $service;
protected $client;
protected $params = [];
public function __construct(Gmail $gmail)
{
$this->client = $gmail;
$this->service = new \Google_Service_Gmail($gmail); // initiate the gmail api service
}
public function list($params = [])
{
$params = array_merge($this->params, $params);
return $this->service->users_messages->listUsersMessages('me', $params);
}
}
Now you can get the gmail messages by
$mails = (new Gmail())->messages()->list();
There are more than 200 api services shipped together on google api. For tree shaking the unnecessary services, please follow instructions on google api client.
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.