namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use LaravelSocialite\GoogleOneTap\LaravelGoogleOneTapServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('laravel-google-one-tap', LaravelGoogleOneTapServiceProvider::class);
});
}
}
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use LaravelSocialite\GoogleOneTap\LaravelGoogleOneTapServiceProvider;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// other providers
LaravelGoogleOneTapServiceProvider::class,
],
];
}
use Laravel\Socialite\Facades\Socialite;
return Socialite::driver('laravel-google-one-tap')->userFromToken($token);
// routes/web.php
use App\Controllers\GoogleOneTapController;
use Illuminate\Support\Facades\Route;
Route::post('google-one-tap', [GoogleOneTapController::class, 'connect'])
->middleware('guest')
->name('google-one-tap.Connect');
// GoogleOneTapController.php
use App\Models\User;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use LaravelSocialite\GoogleOneTap\InvalidIdToEx;
public function connect(Request $request)
{
// Verify and validate JWT received from Google One Tap prompt
try {
$googleUser = Socialite::driver('laravel-google-one-tap')->userFromToken($request->input('credential'));
} catch (InvalidIdToEx $exception) {
return response()->json(['error' => $exception]);
}
// Log the user in if the email is associated with a user
try {
$googleUser = User::where('email', $googleUser['email'])->firstOrfail();
} catch (\Exception $ex) {
$user = User::create([
'name' => $googleUser->getName(),
'lastName' => $googleUser->getNickName(),
'provider_name' => 'google-one-tap',
'email' => $googleUser->getEmail(),
'password' => Hash::make($googleUser->getId()),
'email_verified_at' => $googleUser->user['email_verified'] ? now() : null,
'profile_photo_path' => $googleUser->getAvatar()
]);
Auth::login($user, $remember = true);
return redirect()->route('home');
}
Auth::login($googleUser, $remember = true);
// Send user to Home
return redirect()->route('home');
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.