Support the ongoing development of Laravel.io →
Authentication Security
Last updated 2 years ago.

ostap liked this thread

1

Hello,

I had the same problem, solved it by creating a helper class using the Google PHP API. Unfortunately socialite is pretty bare in functionality.

This is the specific function in my helper class handling refresh tokens, you would have to make some adjustments.

   /**
     * Refresh User's Google OAuth2 Access Token
     * 
     * @param  [type] $oauth2 [description]
     * @return [type]         [description]
     */
    protected static function refreshGoogle($oauth2)
    {
        $config = Config('services.google');
        
        // Config
        $client_id = $config['client_id'];
        $client_secret = $config['client_secret'];

        // User
        $token = $oauth2->{'auth.token'};
        $refreshToken = $oauth2->{'auth.refreshToken'};
        $expiresIn = $oauth2->{'auth.expiresIn'};

        // Time
        $current = Carbon::now();
        $expired = $oauth2->updated_at->addSeconds($expiresIn);

        // If current date exceeds expired date request new access token
        if($current > $expired) {

            // Set Client
            $client = new Google_Client;
            $client->setClientId($client_id);
            $client->setClientSecret($client_secret);
            $client->refreshToken($refreshToken);
            $client->setAccessType('offline');

            return $client->getAccessToken();
        }

        return false;
    }

ostap liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.