Tuesday, 14 February 2023

Steps to consider for login process using google

 web.php

<?php

use App\Http\Controllers\ConnectController;
 
Route::get('/googleconnect', [ConnectController::class, 'googleconnect'])->name('googleconnect');
Route::get('oauth2callback', [ConnectController::class, 'oauth2callback'])->name('oauth2callback'); 


require __DIR__.'/auth.php';

ConnectController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Redirect; use Illuminate\View\View; class ConnectController extends Controller { public function googleconnect(Request $request) { // Initialise the client. $client = new \Google\Client(); $client->setApplicationName('Calendar'); $client->setDeveloperKey("xxxxxxxxx"); $client->setAuthConfig('./client_secret.json'); $client->setAccessType("offline"); $client->setIncludeGrantedScopes(true); $client->setApprovalPrompt('force'); $client->addScope(['profile',\Google_Service_Calendar::CALENDAR]); // Set the redirect URL back to the site to handle the OAuth2 response. 
This handles both the success and failure journeys. $auth_url = $client->createAuthUrl(); $client->setRedirectUri(('http://localhost:8000/') . '/oauth2callback'); return redirect()->to($auth_url); } public function oauth2callback (Request $request) { // Get all the request parameters $input = $request->all();// Attempt to load the venue from the state
we set in $client->setState($venue->id); // the venue with a message. if (isset($input['error']) && $input['error'] == 'access_denied') { toastr()->warning('Authentication was cancelled. Your calendar has not been integrated.'); return redirect()->route('login'); } elseif (isset($input['code'])) { $client = new \Google\Client(); $client->setApplicationName('Calendar'); $client->setDeveloperKey("xxxxxxx"); $client->setAuthConfig('./client_secret.json'); $client->setAccessType("offline"); $client->setIncludeGrantedScopes(true); $client->setApprovalPrompt('force'); $client->addScope(['profile',\Google_Service_Calendar::CALENDAR]); $accessToken = $client->fetchAccessTokenWithAuthCode($request->code); $service = new \Google\Service\Calendar($client); $google_account_email = $service->calendars->get('primary')->id; toastr()->success($google_account_email); return redirect()->route('login'); } } }


Note: Store the access token in database.
Settings

$client->setAccessToken($google->token); // If there is no previous token or it's expired. if ($client->isAccessTokenExpired()) { // Refresh the token if possible, else fetch a new one. if ($client->getRefreshToken()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); } else { // Request authorization from the user. return response()->json(['success' => false, 'message' => 'Something wrong! Please try again.' ], 200); } $tokenObj=json_encode($client->getAccessToken()); $google->token=$tokenObj; $save_token=$google->save(); }


No comments:

Post a Comment

Machine Learning - Potato Leaf Disease Prediction

Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...