laravel login with google code example

Example 1: login with google laravel 7

<?php  namespace App\Http\Controllers\Auth;  use App\Http\Controllers\Controller;use Socialite;use Auth;use Exception;use App\User;  class GoogleController extends Controller{    /**     * Create a new controller instance.     *     * @return void     */    public function redirectToGoogle()    {        return Socialite::driver('google')->redirect();    }          /**     * Create a new controller instance.     *     * @return void     */    public function handleGoogleCallback()    {        try {                $user = Socialite::driver('google')->user();                 $finduser = User::where('google_id', $user->id)->first();                 if($finduser){                     Auth::login($finduser);                    return redirect('/home');                 }else{                $newUser = User::create([                    'name' => $user->name,                    'email' => $user->email,                    'google_id'=> $user->id,                    'password' => encrypt('123456dummy')                ]);                    Auth::login($newUser);                     return redirect('/home');            }            } catch (Exception $e) {            dd($e->getMessage());        }    }}

Example 2: laravel facebook socialite

composer require laravel/socialite

Tags:

Php Example