Skip to content

Instantly share code, notes, and snippets.

@karakanb
Created June 13, 2017 12:23
Show Gist options
  • Save karakanb/7c3f3187f6a54a91ec02355208bd99c5 to your computer and use it in GitHub Desktop.
Save karakanb/7c3f3187f6a54a91ec02355208bd99c5 to your computer and use it in GitHub Desktop.
Laravel - Google Analytics oAuth Implementation
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use Auth;
use Session;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google_Client;
use Google_Service_Analytics;
use Google_Service_Oauth2;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Redirect the user to the Google authentication page.
* This method is accessed by the route 'googleLogin'.
* @return Response
*/
public function redirectToProvider()
{
// Create the client object and set the authorization configuration from JSON file.
$client = new Google_Client();
$client->setAuthConfig(base_path('client_secret.json'));
$client->setRedirectUri(route('googleLoginCallback'));
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
$client->addScope(Google_Service_Analytics::ANALYTICS_MANAGE_USERS);
$client->addScope(Google_Service_Analytics::ANALYTICS_EDIT);
$client->addScope("email");
$client->addScope("profile");
$client->setAccessType("offline");
$auth_url = $client->createAuthUrl();
return redirect($auth_url);
}
/**
* Obtain the user information from Google.
*
* @return Response
*/
public function handleProviderCallback()
{
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
return redirect(route('googleLogin'));
} else {
// Authenticate the client, and get required informations.
$client = new Google_Client();
$client->setAuthConfig(base_path('client_secret.json'));
$client->authenticate($_GET['code']);
$service = new Google_Service_Oauth2($client);
$userInfo = $service->userinfo->get();
$user = User::where('googleID', $userInfo->id)->first();
// If no match, register the user.
if(!$user) {
if($client->getRefreshToken() === NULL) {
$client->revokeToken();
return redirect(route('googleLogin'));
}
$user = new User;
$user->name = $userInfo->name;
$user->googleID = $userInfo->id;
$user->email = $userInfo->email;
$user->refreshToken = $client->getRefreshToken();
$user->code = $_GET['code'];
$user->save();
Session::put('firstTime', TRUE);
} else if($client->getRefreshToken() !== NULL ) {
$user->name = $userInfo->name;
$user->googleID = $userInfo->id;
$user->email = $userInfo->email;
$user->refreshToken = $client->getRefreshToken();
$user->code = $_GET['code'];
$user->save();
}
Auth::login($user);
// Store the tokens in the session.
Session::put($user->id . '_token', $client->getAccessToken());
return redirect(route('home'));
}
}
}
@nam-co
Copy link

nam-co commented Jul 10, 2021

Hi @kakarakanb do you know if this still work? Im trying too make a link that asks permission from google to get analytics records

@karakanb
Copy link
Author

I have no clue, honestly. You'd need to try and see, but I'd guess it wouldn't work

@Tofandel
Copy link

The google lib has changed with namespaces instead of _ now, otherwise all should still work the same

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment