Skip to content
Permalink
Browse files

Fix: token expires after an hour and API throws error on requests

  • Loading branch information...
waleedahmad committed Aug 23, 2019
1 parent 09c5fca commit 9f95888d1ad59a6b454a563e2f47bc47a269f13c
@@ -43,7 +43,10 @@ public function __construct()
public function redirectToGoogleProvider()
{
$parameters = ['access_type' => 'offline'];
$parameters = [
'access_type' => 'offline',
'approval_prompt' => 'force'
];
return Socialite::driver('google')->scopes(["https://www.googleapis.com/auth/drive"])->with($parameters)->redirect();
}
@@ -55,16 +58,23 @@ public function redirectToGoogleProvider()
*/
public function handleProviderGoogleCallback()
{
$auth_user = Socialite::driver('google')->user();
$auth_user = Socialite::driver('Google')->user();
$data = [
'token' => $auth_user->token,
'expires_in' => $auth_user->expiresIn,
'name' => $auth_user->name
];
if($auth_user->refreshToken){
$data['refresh_token'] = $auth_user->refreshToken;
}
$user = User::updateOrCreate(
[
'email' => $auth_user->email
],
[
'refresh_token' => $auth_user->token,
'name' => $auth_user->name
]
$data
);
Auth::login($user, true);
@@ -17,7 +17,27 @@ class DriveController extends Controller
public function __construct(Google_Client $client)
{
$this->middleware(function ($request, $next) use ($client) {
$client->refreshToken(Auth::user()->refresh_token);
$accessToken = [
'access_token' => auth()->user()->token,
'created' => auth()->user()->created_at->timestamp,
'expires_in' => auth()->user()->expires_in,
'refresh_token' => auth()->user()->refresh_token
];
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
auth()->user()->update([
'token' => $client->getAccessToken()['access_token'],
'expires_in' => $client->getAccessToken()['expires_in'],
'created_at' => $client->getAccessToken()['created'],
]);
}
$client->refreshToken(auth()->user()->refresh_token);
$this->drive = new Google_Service_Drive($client);
return $next($request);
});
@@ -15,7 +15,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'refresh_token'
'name', 'email', 'password', 'refresh_token', 'token', 'expires_in'
];
/**
@@ -0,0 +1,34 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddTokenToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('token')->after('refresh_token')->nullable();
$table->integer('expires_in')->after('token')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('token');
$table->dropColumn('expires_in');
});
}
}
@@ -15,6 +15,11 @@
return view('index');
});
Route::get('/logout', function(){
auth()->logout();
return redirect('/');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');

0 comments on commit 9f95888

Please sign in to comment.
You can’t perform that action at this time.