PHP code example of lcmaquino / googleoauth2

1. Go to this page and download the library: Download lcmaquino/googleoauth2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lcmaquino / googleoauth2 example snippets




return [
    'client_id' => env('GOOGLE_CLIENT_ID', ''),
    'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
    'redirect_uri' => env('GOOGLE_REDIRECT_URI', ''),
];

$ php artisan make:controller Auth/LoginController



namespace App\Http\Controllers\Auth;

use Lcmaquino\GoogleOAuth2\GoogleOAuth2Manager;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function redirectToProvider(Request $request)
    {
        $ga = new GoogleOAuth2Manager(config('googleoauth2'), $request);

        return $ga->redirect();
    }

    public function handleProviderCallback(Request $request)
    {
        $ga = new GoogleOAuth2Manager(config('googleoauth2'), $request);
        
        $user = $ga->user();

        if(empty($user)) {
            //$user is not logged in.

            //Do something.
        }else{
            //$user is logged in.

            //Do something.
        }
    }
}

Lcmaquino\GoogleOAuth2\GoogleUser {
    #sub: "1234"
    #name: null
    #email: "[email protected]"
    #emailVerified: true
    #picture: "https://something/with/code"
    #rawAttributes: array:4 [
        "sub" => "1234"
        "picture" => "https://something/with/code"
        "email" => "[email protected]"
        "email_verified" => true
    ]
    #token: "abcd1234"
    #refreshToken: null
    #expiresIn: 3599
}



namespace App\Http\Controllers\Auth;

use GoogleAuth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function redirectToProvider(Request $request)
    {
        return GoogleAuth::redirect();
    }

    public function handleProviderCallback(Request $request)
    {
        $user = GoogleAuth::user();

        if(empty($user)) {
            //$user is not logged in.

            //Do something.
        }else{
            //$user is logged in.

            //Do something.
        }
    }
}

$params = [
    'approval_prompt' => 'force',
];

return GoogleAuth::with($params)->redirect();