Raviya Technical
Published in

Raviya Technical

Laravel with Packages | Laravel Login with Google Account Example

composer create-project --prefer-dist laravel/laravel googleLogin
composer require laravel/socialite
'providers' => [....Laravel\Socialite\SocialiteServiceProvider::class,],'aliases' => [....'Socialite' => Laravel\Socialite\Facades\Socialite::class,],
return [....'google' => ['client_id' => 'app id','client_secret' => 'add secret','redirect' => 'http://learnl52.hd/auth/google/callback',],]
php artisan make:migration add_google_id_column
<?phpuse Illuminate\Support\Facades\Schema;use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class AddGoogleIdColumn extends Migration{/*** Run the migrations.** @return void*/public function up(){Schema::table('users', function ($table) {$table->string('google_id')->nullable();});}/*** Reverse the migrations.** @return void*/public function down(){//}}
<?phpnamespace App;use Illuminate\Notifications\Notifiable;use Illuminate\Contracts\Auth\MustVerifyEmail;use Illuminate\Foundation\Auth\User as Authenticatable;class User extends Authenticatable{use Notifiable;/*** The attributes that are mass assignable.** @var array*/protected $fillable = ['name', 'email', 'password', 'google_id'];/*** The attributes that should be hidden for arrays.** @var array*/protected $hidden = ['password', 'remember_token',];/*** The attributes that should be cast to native types.** @var array*/protected $casts = ['email_verified_at' => 'datetime',];}
Route::get('google', function () {return view('googleAuth');});Route::get('auth/google', 'Auth\LoginController@redirectToGoogle');Route::get('auth/google/callback', 'Auth\LoginController@handleGoogleCallback');
<?phpnamespace App\Http\Controllers\Auth;use App\Http\Controllers\Controller;use Illuminate\Foundation\Auth\AuthenticatesUsers;use Socialite;use Auth;use Exception;use App\User;class LoginController extends Controller{/*|--------------------------------------------------------------------------| Login Controller|--------------------------------------------------------------------------|| This controller handles authenticating users for the application and| redirecting them to your home screen. The controller uses a trait| to conveniently provide its functionality to your applications.|*/use AuthenticatesUsers;/*** Where to redirect users after login.** @var string*/protected $redirectTo = '/home';/*** Create a new controller instance.** @return void*/public function __construct(){$this->middleware('guest')->except('logout');}public function redirectToGoogle(){return Socialite::driver('google')->redirect();}public function handleGoogleCallback(){try {$user = Socialite::driver('google')->user();$finduser = User::where('google_id', $user->id)->first();if($finduser){Auth::login($finduser);return return redirect('/home');}else{$newUser = User::create(['name' => $user->name,'email' => $user->email,'google_id'=> $user->id]);Auth::login($newUser);return redirect()->back();}} catch (Exception $e) {return redirect('auth/google');}}}
<!DOCTYPE html><html><head><title>Laravel 5.8 Login with Google Account Example</title></head><body><div class="container"><div class="row"><div class="col-md-12 row-block"><a href="{{ url('auth/google') }}" class="btn btn-lg btn-primary btn-block"><strong>Login With Google</strong></a></div></div></div></body></html>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store